Compiler reordering around mutex boundaries?

Posted by shojtsy on Stack Overflow See other posts from Stack Overflow or by shojtsy
Published on 2010-04-22T22:38:45Z Indexed on 2010/04/22 22:43 UTC
Read the original article Hit count: 212

Suppose I have my own non-inline functions LockMutex and UnlockMutex, which are using some proper mutex - such as boost - inside. How will the compiler know not to reorder other operations with regard to calls to the LockMutex and UnlockMutex? It can not possibly know how will I implement these functions in some other compilation unit.

void SomeClass::store(int i)
{
  LockMutex(_m);
  _field = i;  // could the compiler move this around?
  UnlockMutex(_m);
}

ps: One is supposed to use instances of classes for holding locks to guarantee unlocking. I have left this out to simplify the example.

© Stack Overflow or respective owner

Related posts about compiler-optimization

Related posts about memory-barriers