July 5th, 2017

How fair are SRW locks, particularly when there are both readers and writers?

A customer wanted to know about the order of lock acquisition in the SRWLOCK functions. They note that the documentation says,

There is no guarantee about the order in which threads that request ownership will be granted ownership; SRW locks are neither fair nor FIFO.

They were worried about starvation. For example, suppose thread 1 acquires in shared mode,¹ and then thread 2 requests exclusive access (and therefore waits). Now thread 3 requests shared access. Will it acquire immediately (since shared mode is available), jumping ahead of thread 2? If readers can jump ahead of writers, then does this mean that thread 2 could be starved out indefinitely if the lock is popular with readers?

As with the other Windows synchronization objects, they do not guarantee fairness. Removing the fairness guarantee avoids lock convoys. That said, SRW locks aren’t aggressively unfair either. If shared readers arrive when there is a waiting writer, they will queue up behind the writer, which generally avoids the problem of readers starving out a writer.

The unfairness comes into play when the last reader exits the lock while there is a waiting writer. At that point, the exclusive waiter is woken so it can acquire the lock, but in the time between the last reader exits and the exclusive waiter acquires, a totally unrelated thread might come in and steal the lock. This behavior is unfortunate, but it’s necessary in order to avoid a lock convoy.

The good news is that even though there is occasional unfairness, there is unlikely to be persistent unfairness. In order for a thread to steal the lock, it needs to hit the tiny window where the lock is available. In practice, a thread is unlikely to be this lucky repeatedly. Eventually, its luck will run out, and it will find that the lock is already acquired, at which point it queues up behind the exclusive writer and will not be able to steal the lock any more.

Reminder: This article discusses implementation details which are not contractual. The behavior may change at any time.

¹ In SRW lock parlance, a shared lock acquirer is often called a “reader” and an exclusive lock acquirer is often called a “writer”.

Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

0 comments

Discussion are closed.