François Trahay
By default, an instruction modifying a variable is non-atomic
example : x++
gives :
register = load(x)
register ++
x = store (register)
→ Problem if the variable is modified by a other thread simultaneously
volatile
?volatile
does not ensure atomicityC11 provides a set of atomic operations, including
atomic_flag_test_and_set
atomic_compare_exchange_strong
atomic_fetch_add
atomic_thread_fence
Performs atomically:
Implementing a lock:
compares *obj
and *expected
if equal, copy desired
into *obj
and
return true
else, copy the value of *obj
into
*expected
and return false
Performs atomically:
C atomic_fetch_add( volatile A* obj, M arg );
obj
with arg+obj
obj
Performs atomically:
C atomic_thread_fence( memory_order order );
Properties to consider when choosing a synchronization primitive
Benefits
test_and_set
)Disadvantages
System call allowing to build synchronization mechanisms in userland
Allows waiting without monopolizing the CPU
A futex is made up of:
Available operations (among others)
WAIT(int *addr, int value)
while(*addr == value) { sleep(); }
WAKE(int *addr, int value, int num)
*addr = value
num
threads waiting on addr
mutex: an integer with two possible values: 1
(unlocked), or 0
(locked)
mutex_lock(m)
:
0
, call FUTEX_WAIT
mutex_unlock(m)
:
FUTEX_WAKE
to wake up a thread from the waiting
listpthread_mutex_timedlock
)Coarse grain locking
Fine grain locking
Each lock protects a small portion of the program
Advantage: possibility of using various resources in parallel
Disadvantages:
[amdahl1967] Amdahl, G. M. (1967, April). Validity of the single processor approach to achieving large scale computing capabilities. In Proceedings of the April 18-20, 1967, spring joint computer conference (pp. 483-485). ISO 690
[coffman1971] Coffman, Edward G., Melanie Elphick, and Arie Shoshani. “System deadlocks.” ACM Computing Surveys (CSUR) 3.2 (1971): 67-78.