L-06
L-06
Concurrency Problems
A statement like w-- in C (or C++) is implemented by several machine
instructions:
ld r4, #w
add r4, r4, -1
st r4, #w
Now, imagine the following sequence, what is the value of w?
Hardware-based solutions
Spinlocks
Blocking synchronization
Solution 1 (Two Threads)
Thread T1 Thread T2
while(Flag[T2]) while(Flag[T1])
; ;
/* enter C.S. */ /* enter C.S. */
/* leave C.S. */ /* leave C.S. */
Flag[T1] = false; Flag[T2] = false;
Solution 3 (Two Threads)
Thread T1 Thread T2
Flag[T1..T2], turn
lock = 0;