#ifndef asp_h #define asp_h #include #include #include #include #include template class stack { public: ~stack() { while (pop()); } void push(T t) { auto n = boost::make_shared(head.load(std::memory_order_relaxed), t); while (!head.compare_exchange_weak( n->next, n, std::memory_order_release, std::memory_order_relaxed)); } std::optional pop() { auto p = head.load(std::memory_order_acquire); while (p && !head.compare_exchange_weak(p, p->next, std::memory_order_release, std::memory_order_relaxed)); if (p) return std::optional(p->data); else return std::nullopt; } private: struct node { boost::shared_ptr next; T data; }; boost::atomic_shared_ptr head; // NO LOCKFREE!!! }; #endif // asp_h