#ifndef stack_h #define stack_h 1 #include #include #include #include #include #include using namespace std::chrono_literals; class spinlock { public: void lock() { auto delay = 50us; while (flag.test_and_set(std::memory_order_acquire)) std::this_thread::sleep_for(delay *= 2); } void unlock() { flag.clear(std::memory_order_release); } private: std::atomic_flag flag; }; template class stack { public: void push(const T &t) { std::lock_guard l(s); container.push(t); } std::optional pop() { std::lock_guard l(s); if (!container.empty()) { auto data = container.top(); container.pop(); return data; } else { return std::nullopt; } } private: spinlock s; std::stack container; }; #endif // stack_h