Using std::mutex and std::lock_guard imitates what a Java synchronized variable does (only in Java it happens secretly, without your knowledge, in C ++ you do it explicitly).
However, having one producer (there is only one direction of the wind), and for the rest only consumers, it is enough to write for example. std::atomic<int> with relaxed ordering and reading from this variable from each consumer again with relaxed ordering. If you do not have a requirement that the global view of all planes be consistent (but then you have to run a lock simulation, which makes threads meaningless), synchronization is not required, you only need to make sure that any value that any planes read at any time are ultimately correct and that no distorted intermediate results can occur. In other words, you need atomic update.
Also, an ordered ordering of memory is not enough, because if everything you read is the same value, you do not need any action - before guarantees.
Atomic updating (more precisely, atomic recording) is at least an order of magnitude, if not more, faster. Atomic readings and records with relaxed ordering are, in fact, the usual normal readings and records on many (most) basic architectures.
This variable does not have to be global; you can also save it in the loop loop area of ββthe main thread and pass a link (or pointer) to the threads.
Damon source share