Consider following Peterson's algorithm implementation from Wikipedia:
flag[0] = 0
flag[1] = 0
turn = 0
P0: flag[0] = 1
turn = 1
memory_barrier( )
while( flag[1] && turn == 1 );
// do nothing
// critical section
...
// end of critical section
flag[0] = 0
P1: flag[1] = 1
turn = 0
memory_barrier( )
while( flag[0] && turn == 0 );
// do nothing
// critical section
...
// end of critical section
flag[1] = 0
We can implement this in Java using volatile variables, and needed
memory_barrier( ) will be emitted automatically by compiler.
We can implement this in C# using volatile variables, and
Thread.MemoryBa rrier() as memory_barrier( ).
We can implement this in x86 MM using plain loads and stores, and
mfence instruction as memory_barrier( ).
We can implement this in C++0x using std::atomic<and issuing loads
with memory_order_ac quire, stores with memory_order_re lease, and
atomic_thread_f ence(memory_ord er_seq_cst) as memory_barrier( ). This is
the most straightforward translation of Java/C#/x86 implementations .
The only problem is that C++0x implementation will not work.
Personally for me, it's quite counter-intuitive. And following
question arise. What is the most simple way to translate some existing
Java/C#/x86 algorithm implementation to C++0x? It seems that it's not
so easy...
Dmitriy V'jukov
flag[0] = 0
flag[1] = 0
turn = 0
P0: flag[0] = 1
turn = 1
memory_barrier( )
while( flag[1] && turn == 1 );
// do nothing
// critical section
...
// end of critical section
flag[0] = 0
P1: flag[1] = 1
turn = 0
memory_barrier( )
while( flag[0] && turn == 0 );
// do nothing
// critical section
...
// end of critical section
flag[1] = 0
We can implement this in Java using volatile variables, and needed
memory_barrier( ) will be emitted automatically by compiler.
We can implement this in C# using volatile variables, and
Thread.MemoryBa rrier() as memory_barrier( ).
We can implement this in x86 MM using plain loads and stores, and
mfence instruction as memory_barrier( ).
We can implement this in C++0x using std::atomic<and issuing loads
with memory_order_ac quire, stores with memory_order_re lease, and
atomic_thread_f ence(memory_ord er_seq_cst) as memory_barrier( ). This is
the most straightforward translation of Java/C#/x86 implementations .
The only problem is that C++0x implementation will not work.
Personally for me, it's quite counter-intuitive. And following
question arise. What is the most simple way to translate some existing
Java/C#/x86 algorithm implementation to C++0x? It seems that it's not
so easy...
Dmitriy V'jukov
Comment