Spin Or Sleep

A microbenchmark on one lock. The critical section takes C=60C = 60 ns. When a waiter blocks instead of spinning, the futex sleep, the later wake, and the two context switches cost H=3.0H = 3.0 microseconds, and that cost is paid once per handoff. Eight threads call the lock at the same instant and each runs the critical section exactly once.

Under a pure spin lock the k-th thread to acquire waits

tspin(k)=(k−1) Ct_{\text{spin}}(k) = (k-1),C

and under a pure sleeping lock each handoff also pays the block-and-wake cost

tsleep(k)=(k−1) (C+H)t_{\text{sleep}}(k) = (k-1),(C + H)

How long does the last of the eight threads wait under each policy?

A

60 ns spinning against 3.06 microseconds sleeping, a factor of about 51.

B

420 ns spinning against 21.4 microseconds sleeping, a factor of about 51.

C

420 ns spinning against 3.0 microseconds sleeping, a factor of about 7.

D

480 ns spinning against 24.5 microseconds sleeping, a factor of about 51.