multithreading - How is the atomic unlock-and-block achieved in the implementation of synchronization primitives like mutexes and condition variables? -
for example, suppose using atomic spinlock on integer flag ensure 1 thread modifies wait-queue mutex maintains @ given time. when thread tries lock mutex, want enqueue , set flag 0 before blocks , unlocker dequeue thread queue , set runnable.
consider 2 threads present, 1 locking , other releasing mutex @ same time. if locker preempted after added himself queue , set flag 0 (but not blocked yet) , unlocker tried dequeue , make thread runnable, wouldn't useful since thread hasn't blocked yet. make-runnable call waste more importantly, locker thread block after , remain blocked forever.
how atomicity achieved ensure correctness? similar scenario can imagined in condition variables release of mutex , blocking itself.
Comments
Post a Comment