Here is some C ++ code that is accessed from multiple threads in parallel. It has a critical section:
lock.Acquire();
current_id = shared_id;
shared_id = (shared_id + 1) % max_id;
lock.Release();
The lock variable class is the shell of the POSIX mutex implementation. Due to module operations, atomic operations cannot be used.
Is it possible that the gcc compiler with the O3 flag optimizes the code so that the current_id assignment is moved before locking?
source
share