Ensuring usage of double-compare-and-swap instruction, for lock-free stack?
问题 (Assume 64-bit x86-64 architecture and Intel 3rd/4th generation CPU) Here is a lock-free implementation for a stack from Concurrency in Action book, page 202: template<typename T> class lock_free_stack { private: struct node; struct counted_node_ptr { int external_count; node* ptr; }; struct node { std::shared_ptr<T> data; std::atomic<int> internal_count; counted_node_ptr next; node(T const& data_):data(std::make_shared<T>(data_)),internal_count(0){} }; std::atomic<counted_node_ptr> head;