A race
Two threads share one pointer, info, which points at the text msg. Thread 1 prints it if it is not NULL. Thread 2 sets it to NULL. Each line is correct on its own. Ignore the faint lock lines for now.
The scheduler runs Thread 1 on the CPU. It checks info != NULL. info points at msg, so the check passes, and Thread 1 moves on to its fputs line.
Before fputs runs, a timer interrupt fires and the scheduler switches the CPU to Thread 2. Thread 2 sets info to NULL. Thread 1 is paused between its check and its use, so it cannot notice.
Thread 1 resumes at fputs. Its check said yes, but info is now NULL, so fputs reads through a NULL pointer and the program crashes. This is an atomicity violation: the check and the use were meant to happen as one.
Fixing it
Run it again with one lock, m, around both. Thread 1 takes m, then the switch comes. Thread 2's lock(&m) waits until Thread 1 unlocks. The check and the use now run together, and Thread 2 clears info afterwards.
A study of 105 concurrency bugs in MySQL, Apache, Mozilla and OpenOffice found 74 non-deadlock bugs and 31 deadlocks. Of the non-deadlock bugs, 97% were atomicity violations like this one, or order violations: using something before another thread sets it.
Events
Event-based concurrency drops the threads. One loop asks the kernel which events are ready, using select(), poll() or epoll, then runs a handler for each, one at a time. The same two jobs, print and clear, arrive as events. info points at msg again.
The loop takes the print event and runs its handler from start to finish: check, then print msg. Only then does it run the clear handler. On one CPU nothing can cut in between, so no lock is needed.
A third event needs a file. Its handler calls read(), which waits for the disk. With only one thread, the whole loop waits too: a new print event sits in the inbox, unanswered, until the disk replies.
Instead the handler starts the read with aio_read(), saves what it still needs, and returns at once. The loop serves the waiting event. When the disk finishes, a completion event arrives, and a second handler finishes the job from the saved state.
Threads need locks and care about order. Events need no locks on one CPU, but no handler may block, and each must save its own state. To use several CPUs, handlers run in parallel, and the locks come back.
What happens
;; step 1/11 · two-threads
shared info -> "msg" (a pointer to some text)
thread 1 if (info != NULL) check
fputs(info, out); use
thread 2 info = NULL; clear
a race: two threads use one memory location,
and at least one of them writes it (MIT 6.1810)
from MySQL (OSTEP fig. 32.2): thd->proc_info
The two threads are the MySQL bug from OSTEP chapter 32, with thd->proc_info shortened to info. lock() and unlock() on the cards are one-line wrappers around pthread_mutex_lock() and pthread_mutex_unlock(), so the lines stay short. The figure has one CPU. With several CPUs, two threads can run truly at the same time, so the race needs no timer interrupt at all, and event handlers spread over cores need locks again.