Common variables in C ++ 11

So, I took the OS class last semester, and we had a concurrency / threading project. It was an airport simulator that landed on airplanes / made them fly in the direction the wind was blowing. We had to do this in Java. So now that the finals are over and I'm bored, I'm trying to do it in C ++ 11. In Java I used a synchronized variable for wind (0 - 360) basically and passed it to the 3 threads I used. My question is: can you do this in C ++ 11? This is the main reader / writer, one thread writes / updates the wind, the other 2 (take-off / ground) are read.

I got it working by having a global wind variable in my threads.cpp implementation file. But is there a way to pass a variable as many threads as I need, and they all keep up with it? Or is it best for me to use a global variable and not skip anything? (Why / why not?) I looked at std::ref() , but that didn't work.

EDIT: I already use mutex and lock_guard. I'm just trying to figure out how to go through and keep the variable up to date in all threads. Now it is updated only in the recording stream.

+5
source share
3 answers

You can use std::mutex with std::lock_guard to synchronize access to shared data. Or, if the shared data fits into an integer, you can use std::atomic<int> without blocking.

If you want to avoid global variables, just pass the general state address to the stream functions when they start. For instance:

 void thread_entry1(std::atomic<int>* val) {} void thread_entry2(std::atomic<int>* val) {} std::atomic<int> shared_value; std::thread t1(thread_entry1, &shared_value); std::thread t2(thread_entry2, &shared_value); 
+5
source

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.

+2
source

You might want to create, say, a wind object in the new heap through std::shared_ptr . Pass this pointer to all interested threads and use std::mutex and std::lock_guard to change it.

+1
source

Source: https://habr.com/ru/post/1238776/


All Articles