c++ - Statement reordering with locks -
Here are some C ++ code which is accessed from multiple threads in parallel. There is an important section in it:
lock.Acquire (); Current_id = shared_id; // Less amount of other code shared_id = (shared_id + 1)% max_id; Lock.Release (); // do something with current_id
The class of lock variable is wrapped around the POSIX mutation implementation. Due to module operations, it is not possible to use nuclear operations.
Is it possible that a GCC compiler with the O3 flag optimizes the code so that the current_id's assignment can be transferred before lock?
It is possible to compile from O3!
Make a function call the compiler until the function is marked as pure using function-attributes.
Mutex functions are not pure, so they are perfectly safe to use with O3.
Comments
Post a Comment