9. Атомарные операции ( R ead- M odify- W rite) unsigned long a, b = 1; a = __sync_fetch_and_add (&a, 1); mov $0x1,%edx lock xadd %rdx,(%rax)
10. Атомарные операции ( C ompare- A nd- S wap) unsigned long val = 5; __sync_val_compare_and_swap (&val, 5, 2); mov $0x5,%eax mov $0x2,%ecx lock cmpxchg %rcx,(%rdx)
11.
12. Lock-free очередь (список) push (ff: pointer to fifo, cl: pointer to cell): Loop: cl->next = ff->tail if CAS (&ff->tail, cl->next, cl): break
13. Lock-free очередь (список) pop (ff: pointer to fifo): Loop: cl = ff->head next = cl->next if CAS (&ff->head, cl, next): break return cl
14.
15. Lock-free очередь: ABA pop (ff: pointer to fifo): Loop: cl = ff->head next = cl->next # cl = A, next = B ------------------->scheduled # pop(A), pop(B), push(A) if CAS (&ff->head, cl, next) : # cl->head => B (вместо C) break return cl