mercredi 1 mai 2019

Multithread synchnization using std::atomic - value change of std::atomic in "subthread" not registered in main thread

I'm working on multithreaded application in Visual Studio 2017 and I use std::atomic_bool variable for synchnization. (I know that using std::mutex would be much better practice, but I haven't found a way to check std::mutex value without locking it.)

The synchnization works fine if I work only with main thread a one secondary thread executing asynchronous task AsyncTask1 (MainThread()->SecondaryThread(AsyncTask1())). bool variable is set to true in the MainThread before the beginning and to false at the end of SecondaryThread execution and change is visible in the MainThread.

However if I perform the AsyncTask in more nested thread (MainThread()->SecondaryThread(AsyncTask2())->TertiaryThread(AsyncTask1())), the unlocking of std::atomic_bool variable is registered in SecondaryThread but not in MainThread. I do find that quite confusing, since locking was registered.

In nested case, address of class object, part of which is the std::variable is passed to the SecondThread and address of the std::variable is passed to the ThirdThread.

Do you have experience with such beaviour? Is it valid behaviour?

Thanks

Aucun commentaire:

Enregistrer un commentaire