Why do we need this additional check in this concurrent queue implementation?

Working through a textbook which suggests the following implementation for a concurrent queue. I am wondering why the two if statements, which are highlighted in the code, are necessary. After the snippet I include some reasoning about why I don’t think they are necessary, but I’m still hesitant to go against the word of my textbook, so I would love some explanation about why we need these.

Please feel free to ignore my “proof” of why we don’t need the if statements: just trying to give some insight into how I’m thinking about the problem.

The implementation:

template<typename T>
struct Node
{
    Node(T val, Node* next = nullptr)
    : value(val), next(next)
    {}
    T value;
    std::atomic<Node*> next;
};

template<typename T>
struct UnboundedQueue
{
    std::atomic<Node*> head;
    std::atomic<Node*> tail;
    void enq(T val);
    T deq(void);
};

template<typename T>
void UnboundedQueue::enq(T val)
{
    Node* node = new Node(val);

    while(true)
    {
        Node* last = tail.load();
        Node* next = last->next.load();

// ********* THIS if statement
        if(last == tail.load())
        {
            if(next == nullptr)
            {
                if(last->next.compare_exchange_weak(next, node))
                {
                    tail.compare_exchange_weak(last, node);
                    return;
                }
            }
            else
            {
                tail.compare_exchange_weak(last, next);
            }
        }
    }
}

template<typename T>
std::optional<T> UnboundedQueue::deq(void)
{
    while(true)
    {
        Node* first = head.load();
        Node* last = tail.load();
        Node* next = first->next.load();
// ********* and THIS if statement
        if(first == head.load())
        {
            if(last == first)
            {
                if(first->next == nullptr)
                {
                    return std::nullopt;
                }
                else
                {
                    tail.compare_exchange_weak(last, next);
                }
            }
            else
            {
                if(head.compare_exchange_weak(first, next))
                {
                    T res = first->value;            
                    // this is UB but question isn't about that
                    delete first;
                    return res;
                }
            }
        }
    }
}

Intuitively, these checks feel unnecessary.

Firstly, we note that we have the following two invariants:

  1. The head node never moves past the tail node.

Informal Proof: To have this be the case we would need that head == tail and that head == first (to execute the CAS). But as we load last after loading first, this would mean that last == tail, and so we would enter the first if statement and not move head past tail.

  1. If a node was ever the tail node and its next value is nullptr, then it is currently the tail node.

Informal Proof: We never set the next value of a node to nullptr. So for a node to lose its status as the tail we would first need to add another node after it, setting its next value to not nullptr.

And so now we assume that we remove the if statements and enter each of the methods with the following conditions being true, showing that in each case, nothing incorrect happens to our state:

Assume that last != tail.load()

Case 1: next == nullptr and by our second invariant we have that our assumption must be false.

Case 2: next != nullptr. Then we perform tail.compare_exchange_weak(last, next), which will do nothing because tail != last

Now assume that first != head.load()

Case 1: last == first
>>Case 1.1: first->next == nullptr.
So last == first, and therefore first was once equal to tail. And therefore by our second invariant we have that first == tail and so, by our second invariant, as our head never moves past tail, we must have that first == head and so our assumption is false.
>>Case 1.2: first->next != nullptr
>>>Case 1.2.1: first != tail: and so tail.compare_exchange_weak(last, next) does nothing.
>>>Case 1.2.2: first == tail: and therefore first == head, as the head can only move forward, but cannot move forward past the tail by our first invariant.

Case 2: last != first:
And we will perform head.compare_exchange_weak(first, next), which will do nothing because head != first (by assumption)

I’ll note that I don’t have a ton of experience with concurrency, so certainly could have made a mistake somewhere.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật