Search code examples
c++concurrencyc++17

What is the meaning of this 'for' loop in the lock free queue implementation in the book "C++ Concurrency in Action"?


I am reading Anthony Williams's "C++ Concurrency in Action, Second Edition", and I encountered this code:

template<typename T>
class lock_free_queue
{
private:
    void set_new_tail(counted_node_ptr &old_tail,
                      counted_node_ptr const &new_tail)
    {
        node* const current_tail_ptr=old_tail.ptr;
        while(!tail.compare_exchange_weak(old_tail,new_tail) &&
              old_tail.ptr==current_tail_ptr);
        if(old_tail.ptr==current_tail_ptr)
            free_external_counter(old_tail);
        else
            current_tail_ptr->release_ref();
    }
public:
    void push(T new_value)
    {
        std::unique_ptr<T> new_data(new T(new_value));
        counted_node_ptr new_next;
        new_next.ptr=new node;
        new_next.external_count=1;
        counted_node_ptr old_tail=tail.load();
        for(;;)
        {
            increase_external_count(tail,old_tail);
            T* old_data=nullptr;
            if(old_tail.ptr->data.compare_exchange_strong(
                   old_data,new_data.get()))
            {
                counted_node_ptr old_next={0};
                if(!old_tail.ptr->next.compare_exchange_strong(
                       old_next,new_next))
                {
                    delete new_next.ptr;
                    new_next=old_next;
                }
                set_new_tail(old_tail, new_next);
                new_data.release();
                break;
            }
            else
            {
                counted_node_ptr old_next={0};
                if(old_tail.ptr->next.compare_exchange_strong(
                       old_next,new_next))
                {
                    old_next=new_next;
                    new_next.ptr=new node;
                }
                set_new_tail(old_tail, old_next);
            }
        }
    }
};

The remaining part is in GitHub, which is from listing_7.19.cpp to listing_7.22.cpp.

Here I encounter some problems, that I can't understand this part:

for(;;)
        {
            increase_external_count(tail,old_tail);
            T* old_data=nullptr;
            if(old_tail.ptr->data.compare_exchange_strong(
                   old_data,new_data.get()))
            {
                counted_node_ptr old_next={0};
                if(!old_tail.ptr->next.compare_exchange_strong(
                       old_next,new_next))
                {
                    delete new_next.ptr;
                    new_next=old_next;
                }
                set_new_tail(old_tail, new_next);
                new_data.release();
                break;
            }
            else
            {
                counted_node_ptr old_next={0};
                if(old_tail.ptr->next.compare_exchange_strong(
                       old_next,new_next))
                {
                    old_next=new_next;
                    new_next.ptr=new node;
                }
                set_new_tail(old_tail, old_next);
            }
        }

First, I think old_tail is something not null, why does it compare this to old_data (which is a nullptr) in this statement?

if(old_tail.ptr->data.compare_exchange_strong(
                   old_data,new_data.get()))

And, what is the above for loop actually doing?

I completely can't understand this.


Solution

  • If the question is why put this:

    T* old_data=nullptr;
    if(old_tail.ptr->data.compare_exchange_strong(old_data,new_data.get()))
              //...
    

    Rather than say this:

    if(old_tail.ptr->data.compare_exchange_strong(nullptr,new_data.get()))
             //...
    

    That's because compare_exchange_strong() returns the value at the point of compare in the first argument so it requires a modifiable l-value (a reference). In the specimen code the value returned isn't used. But there isn't a version of the member that accepts a constant.

    If the question is "what is the for(;;) loop doing?"

    The answer is using compare_exchange_strong() to try and be the thread that either populates data in the tail or adds a new tail to populate. The logic is lockless (if the relevant atomic operations are lockless on the target platform - not guaranteed) but the downside is that may require several attempts (iterations) for a thread to be 'the winner' and under high load there's a risk of 'live lock' behaviour because it's not guaranteed when a given thread will succeed. Lock-free implementations sound attractive but sometimes come with downsides that can be worse than locking and completing the work in a single pass. Of course introducing a lock in the form of a std::mutex doesn't guarantee any fairness and (again) high load scenarios may result in live-lock behaviours. But the point remains that the implementation shown isn't necessarily a silver bullet.

    This code demonstrates the first point:

    #include <iostream>
    #include <atomic>
    
    int main() {
        int value1{1};
        int value2{2};
        std::atomic<int *> x(&value1);
        int * e=nullptr;
        
        auto r{x.compare_exchange_strong(e,&value2)};
        
        std::cout<<r<<' '<<e<<' '<<&value1<<'\n';
        
        return 0;
    }
       
    

    Typical Output:

    0 0x7ffe378b5340 0x7ffe378b5340
    

    Notice that the address returned for e is the address of value1.