Who Writes Turn
A ticket lock. Note that ticket is bumped with an atomic fetch-and-add but turn is bumped with a plain non-atomic read-modify-write:
typedef struct { int ticket; int turn; } lock_t;
void lock(lock_t *lk) {
int myturn = FetchAndAdd(&lk->ticket); // atomic
while (lk->turn != myturn)
; // spin
}
void unlock(lock_t *lk) {
lk->turn = lk->turn + 1; // plain increment
}
Sixteen threads hammer this lock. Why does the plain increment in unlock never lose an update?
Sign in to answer questions and track your progress
Sign In