vendredi 28 décembre 2018

Will Mutex protection failed for register promotion?

In an article about c++11 memory order, author show an example reasoning "threads lib will not work in c++03"

for (...){
  ...
  if (mt) pthread_mutex_lock(...);
  x=...x...
  if (mt) pthread_mutex_unlock(...);
}
//should not have data-race
//but if "clever" compiler use a technique called 
//"register promotion" , code become like this:

r = x;
for (...){
    ...
if (mt) {
    x=r; pthread_mutex_lock(...); r=x;
}
r=...r...
if (mt) {
    x=r; pthread_mutex_unlock(...); r=x;
}
x=r;

There are 3 question:

1.Is this promotion only break the mutex protection in c++03?What about c language?

2.c++03 thread libs become unwork?

3.Any other promotion may caused same problem?

Aucun commentaire:

Enregistrer un commentaire