Unexpected Behavior with compare_exchange_strong in Single Producer Multi Consumer Queue Implementation

I’m working on a single-producer, multi-consumer lock-free queue implementation in C++. The code is based on examples from the book Concurrency in Action by Anthony Williams. However, I have encountered an issue with compare_exchange_strong that I can’t quite explain. Even with only one push thread, compare_exchange_strong occasionally fails in a way that suggests it was interrupted or otherwise preempted. Here is a simplified version of my code:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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;
// Create a new tail node
counted_node_ptr old_tail = tail.load();
for (;;) {
increase_external_count(tail, old_tail);
T* old_data = nullptr;
// Attempt to set the data of the current tail node
while (!old_tail.ptr->data.compare_exchange_weak(
old_data, new_data.get()) && old_data == nullptr);
if (old_data == nullptr) {
// Successfully replaced the old tail's data with new data
counted_node_ptr old_next = { 0, nullptr };
// Attempt to update the next pointer of the current tail node
while (!old_tail.ptr->next.compare_exchange_weak(
old_next, new_next) && (old_next.external_count == 0 && old_next.ptr == nullptr));
if (!(old_next.external_count == 0 && old_next.ptr == nullptr)) {
// Update the next pointer of the current tail node failed unexpectedly
assert(0);
delete new_next.ptr;
new_next.ptr = nullptr;
new_next = old_next;
}
// Set the new tail node
set_new_tail(old_tail, new_next);
new_data.release();
break;
} else {
// Another thread has already inserted data
// Retry with updated tail
}
}
}
</code>
<code>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; // Create a new tail node counted_node_ptr old_tail = tail.load(); for (;;) { increase_external_count(tail, old_tail); T* old_data = nullptr; // Attempt to set the data of the current tail node while (!old_tail.ptr->data.compare_exchange_weak( old_data, new_data.get()) && old_data == nullptr); if (old_data == nullptr) { // Successfully replaced the old tail's data with new data counted_node_ptr old_next = { 0, nullptr }; // Attempt to update the next pointer of the current tail node while (!old_tail.ptr->next.compare_exchange_weak( old_next, new_next) && (old_next.external_count == 0 && old_next.ptr == nullptr)); if (!(old_next.external_count == 0 && old_next.ptr == nullptr)) { // Update the next pointer of the current tail node failed unexpectedly assert(0); delete new_next.ptr; new_next.ptr = nullptr; new_next = old_next; } // Set the new tail node set_new_tail(old_tail, new_next); new_data.release(); break; } else { // Another thread has already inserted data // Retry with updated tail } } } </code>
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;

    // Create a new tail node
    counted_node_ptr old_tail = tail.load();

    for (;;) {
        increase_external_count(tail, old_tail);
        T* old_data = nullptr;

        // Attempt to set the data of the current tail node
        while (!old_tail.ptr->data.compare_exchange_weak(
            old_data, new_data.get()) && old_data == nullptr);

        if (old_data == nullptr) {
            // Successfully replaced the old tail's data with new data
            counted_node_ptr old_next = { 0, nullptr };

            // Attempt to update the next pointer of the current tail node
            while (!old_tail.ptr->next.compare_exchange_weak(
                old_next, new_next) && (old_next.external_count == 0 && old_next.ptr == nullptr));

            if (!(old_next.external_count == 0 && old_next.ptr == nullptr)) {
                // Update the next pointer of the current tail node failed unexpectedly
                assert(0);
                delete new_next.ptr;
                new_next.ptr = nullptr;
                new_next = old_next;
            }

            // Set the new tail node
            set_new_tail(old_tail, new_next);
            new_data.release();
            break;
        } else {
            // Another thread has already inserted data
            // Retry with updated tail
        }
    }
}

test conditions:
Single push thread and one pop thread.
No other concurrent operations modifying tail or next pointers.

In the code, compare_exchange_weak is used in a loop to handle potential spurious failures. However, I expected compare_exchange_strong to be resilient to such issues without needing a loop. Despite this, when I replace compare_exchange_weak with compare_exchange_strong, the assertion in the else branch triggers, even though there is only one push thread and no other thread should be interfering with the push operation.

Unexpected Behavior with compare_exchange_strong in Single Producer Multi Consumer Queue Implementation

Why does compare_exchange_strong fail in this context even though there is no concurrent push operation?
Is there a fundamental difference in how compare_exchange_strong and compare_exchange_weak handle interruptions that I might be missing?
What are the best practices for using compare_exchange_strong and compare_exchange_weak in a single-producer, multi-consumer queue?

Note:
The implementation is copied from the book Concurrency in Action by Anthony Williams.
running on an x86-64 architecture.
using the GCC 9 compiler

Why does compare_exchange_strong fail in this context even though there is no concurrent push operation?
Is there a fundamental difference in how compare_exchange_strong and compare_exchange_weak handle interruptions that I might be missing?
What are the best practices for using compare_exchange_strong and compare_exchange_weak in a single-producer, multi-consumer queue?

New contributor

user25444684 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

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