Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 implementation. 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?

like image 377
dmeister Avatar asked Oct 15 '22 10:10

dmeister


2 Answers

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.

like image 58
Nils Pipenbrinck Avatar answered Oct 26 '22 19:10

Nils Pipenbrinck


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.

like image 33
Kosi2801 Avatar answered Oct 26 '22 18:10

Kosi2801