#include #include template class lock_free_stack { private: struct node { std::shared_ptr data; std::shared_ptr next; node(T const& data_): data(std::make_shared(data_)) {} }; std::shared_ptr head; public: void push(T const& data) { std::shared_ptr const new_node=std::make_shared(data); new_node->next=head.load(); while(!std::atomic_compare_exchange_weak( &head,&new_node->next,new_node)); } std::shared_ptr pop() { std::shared_ptr old_head=std::atomic_load(&head); while(old_head && !std::atomic_compare_exchange_weak( &head,&old_head,old_head->next)); return old_head ? old_head->data : std::shared_ptr(); } };