mardi 12 juin 2018

C++11 reading a bool concurrently

When I have a std::condition_variable cond and some bool flag I can wait for it using a predicate:

cond.wait_for(some_lock, std::chrono::milliseconds(100), { return flag; })

Now I am wondering: Technically C++ only got a proper multi threaded memory model in C++11, and accessing the flag variable in a multi threaded context is basically undefined. So I should have to declare it std::atomic<bool> to escape that undefined behavior, right?

I am specifically wondering: If I'm NOT declaring it std::atomic, can it be that I read stale values from flag all the time as the update never makes it to main memory? Or is this a case of "theoretically yes, but practically never happens"?

Aucun commentaire:

Enregistrer un commentaire