Statement reordering with locks
Here some C++ code that is accessed from multiple threads in parallel. It has a critical section:
lock.Acquire();
current_id = shared_id;
// small amounts of other code
shared_id = (shared_id + 1) % max_id;
lock.Release();
// do something with current_id
The class of the lock variable is wrapper around the POSIX mutex implement开发者_JAVA技巧ation. Because of the module operations, it is not possible to use atomic operations.
Is it possible that a gcc compiler with a O3 flag optimizes the code so that the assignment of current_id is moved before the lock?
It is possible to compile with O3!
The compiler will never optimize across a function call unless the function is marked as pure using function-attributes.
The mutex functions aren't pure, so it's absolutely safe to use them with O3.
Normally the compiler should not make such harmful optimizations. If you're still unsure, you could use the volatile keyword to prevent optimizations on that id variables.
精彩评论