#ifndef stack_h #define stack_h 1 #include #include #include #include #include #include using namespace std::chrono_literals; template class stack { public: void push(T t) { std::unique_lock lock(mutex); cv.wait(lock, [&] { return lock.owns_lock(); }); container.push(std::move(t)); lock.unlock(); cv.notify_all(); } std::optional pop() { std::optional optional; std::unique_lock lock(mutex); cv.wait(lock, [&] { return lock.owns_lock(); }); if (!container.empty()) { optional = container.top(); container.pop(); } lock.unlock(); cv.notify_all(); return optional; } private: std::mutex mutex; std::condition_variable cv; std::stack container; }; #endif // stack_h