#include template class lock_free_queue { private: struct node; struct counted_node_ptr { int external_count; node* ptr; }; std::atomic head; std::atomic tail; struct node_counter { unsigned internal_count : 30; unsigned external_counters : 2; }; struct node { std::atomic data; std::atomic count; counted_node_ptr next; node() { node_counter new_count; new_count.internal_count = 0; new_count.external_counters = 2; count.store(new_count); next.ptr = nullptr; next.external_count = 0; } }; public: void push(T new_value) { std::unique_ptr 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())) { old_tail.ptr->next = new_next; old_tail = tail.exchange(new_next); free_external_counter(old_tail); new_data.release(); break; } old_tail.ptr->release_ref(); } } };