A Corrected One-Producer, One-Consumer Lock-Free Queue
Now let's tackle the lock-free queue using our essential tools. In this first take, to allow easier comparison with the original code in [2], I'll stay fairly close to the original design and implementation, including that I'll continue to make the same simplifying assumption that there is exactly one Consumer thread and one Producer thread, so that we can easily arrange for them to always work in different parts of the underlying linked list. In Figure 1, the first "unconsumed" item is the one after the divider
. The consumer increments divider
to say it has consumed an item. The producer increments last
to say it has produced an item, and also lazily cleans up consumed items before the divider
.
Here's the class definition, which carefully marks shared variables as being of an ordered atomic type (using C++ to most closely follow the original code in [2]):
template <typename T> class LockFreeQueue { private: struct Node { Node( T val ) : value(val), next(nullptr) { } T value; Node* next; }; Node* first; // for producer only atomic<Node*> divider, last; // shared
The constructor simply initializes the list with a dummy element. The destructor (in C# or Java, the dispose
method) releases the list. In a future column, I'll discuss in detail why constructors and destructors of a shared object don't need to worry about concurrency and races with methods of the same object; the short answer for now is that creating or tearing down an object should always run in isolation, so no internal synchronization needed.
public: LockFreeQueue() { first = divider = last = new Node( T() ); // add dummy separator } ~LockFreeQueue() { while( first != nullptr ) { // release the list Node* tmp = first; first = tmp->next; delete tmp; } }
Next, we'll look at the key methods, Produce
and Consume
. Figure 2 shows another view of the list by who owns what data by color-coding: The producer owns all nodes before divider
, the next
pointer inside the last
node, and the ability to update first
and last
. The consumer owns everything else, including the values in the nodes from divider
onward, and the ability to update divider
.
The Producer
Produce
is called on the producer thread only:
void Produce( const T& t ) { last->next = new Node(t); // add the new item last = last->next; // publish it while( first != divider ) { // trim unused nodes Node* tmp = first; first = first->next; delete tmp; } }
First, the producer creates a new Node
containing the value and links it to the current last
node. At this point, the node is not yet shared, but still private to the producer thread even though there's a link to it; the consumer will not follow that link unless the value of last
says it may follow it. Finally, when all the real work is donethe node exists, its value is completely initialized, and it's correctly connectedthen, and only then, do we write to last
to "commit" the update and publish it atomically to the consumer thread. The consumer reads last
, and either sees the old value (and ignores the new partly constructed element even if the last->next
pointer might already have been set) or the new value that officially blesses the new node as an approved part of the queue, ready to be used.
Finally, the producer performs lazy cleanup of now-unused nodes. Because we always stop before divider
, this can't conflict with anything the consumer might be doing later in the list. What if while we're in the loop, the consumer is consuming items and changing the value of divider
? No problem: Each time we read divider
, we see it either before or after any concurrent update by the consumer, both of which let the producer see the list in a consistent state.
The Consumer
Consume
is called on the consumer thread only:
bool Consume( T& result ) { if( divider != last ) { // if queue is nonempty result = divider->next->value; // C: copy it back divider = divider->next; // D: publish that we took it return true; // and report success } return false; // else report empty } };
First, the consumer checks that the list is nonempty by atomically reading divider
, atomically reading last
, and comparing them. This one-time check is safe because although last
's value may be changed by the producer while we are running the rest of this method, if the check is true once, it will stay true even if last
moves, because last
never backs up; it can only move forward to publish new tail nodeswhich doesn't affect the consumer, who only cares about the first node after the divider
. If there is a valid node after divider
, the consumer copies its value and then, finally, advances divider
to publish that the queue item was removed.
Yes, we could eliminate the need to make the last
variable shared: The consumer only uses the value of last
to check whether there's another node after the divider
, and we could instead have the consumer just test whether divider->nex
t is non-null. That would be fine, and it would let us make last
an ordinary variable; but if we do that, we must also remember that this change would make each next
member a shared variable instead, and so to make it safe, we would also have to change next
's type to atomic<Node*>
. I'm leaving last
as is for now to make it easier to compare this code with the original version in [2], which did use such a tail iterator to communicate between two threads.