operations that involve volatile variables are not reordered with
respect to other volatile variables. Note that when mixing normal and
volatile variables, memory fences are instead required (compiler
fences on Intel arch).
Writes to x and y cannot be rearranged because both are volatile.
This should explain why that assert does not fire.
To implement a better synchronization between threads you may want to
use <atomic> anyway.
Nicola
--
The difference between theory and practice is bigger in practice than in theory.
flag1 in p0, so p0 may or may not enter the while loop. If p0 reads the value of false for flag1, it will not enter the while loop, and will instead enter the critical section, but that is OK since p1 has entered the while loop."Yes.
> ////////
>
> p0 -> while (flag1.load(std::memory_order_relaxed)) // p0.flag0 = true,
> p0.flag1 = false;
>
> Here is the part i do not fully understand : "On the other side, there
> is no such guarantee for the read from |flag1| in |p0|, so |p0| may or
> may not enter the |while| loop. If |p0| reads the value of |false| for
> |flag1|, it will not enter the |while| loop, and will instead enter the
> critical section, but that is OK since |p1| has entered the |while| loop."
>
> With the thread_fence before, p0 should be able to retrieve the correct
> version of flag1 ?
No. The fence in p0 is before the fence in p1 in the SC ordering.
Therefore there is no guarantee that anything from p1 is visible in p0
at this point.
Anthony
--
Author of C++ Concurrency in Action http://www.stdthread.co.uk/book/
just::thread C++0x thread library http://www.stdthread.co.uk
Just Software Solutions Ltd http://www.justsoftwaresolutions.co.uk
15 Carrallack Mews, St Just, Cornwall, TR19 7UL, UK. Company No. 5478976
You're welcome.
> Just to be sure i have understood everything :
>
> In the example, we know p0 fence is before p1 fence. So p0 will go
> through the fence, but may not have any informations on p1 flags state.
> Since p1 fence come after p0 fence (as in your comment, p0 may have
> completed his cycle), it will surely know everything about p0 flags state.
>
> Is it correct ?
Yes.