#ifndef mutex_h #define mutex_h #include #include #include #include using namespace std::chrono_literals; template class stack { public: void push(T t) { auto backoff = 1us; while (!mutex.try_lock()) std::this_thread::sleep_for(backoff *= 2); container.push(t); mutex.unlock(); } std::optional pop() { auto backoff = 1us; while (!mutex.try_lock()) std::this_thread::sleep_for(backoff *= 2); if (!container.empty()) { T data = container.top(); container.pop(); mutex.unlock(); return std::optional(data); } else { mutex.unlock(); return std::nullopt; } } private: std::mutex mutex; std::stack container; }; #endif // mutex_h