#include #include #include #include struct empty_stack: std::exception { const char* what() const throw() { return "empty stack"; } }; template class threadsafe_stack { private: std::stack data; mutable std::mutex m; public: threadsafe_stack(){} threadsafe_stack(const threadsafe_stack& other) { std::lock_guard lock(other.m); data=other.data; } threadsafe_stack& operator=(const threadsafe_stack&) = delete; void push(T new_value) { std::lock_guard lock(m); data.push(new_value); } std::shared_ptr pop() { std::lock_guard lock(m); if(data.empty()) throw empty_stack(); std::shared_ptr const res(std::make_shared(data.top())); data.pop(); return res; } void pop(T& value) { std::lock_guard lock(m); if(data.empty()) throw empty_stack(); value=data.top(); data.pop(); } bool empty() const { std::lock_guard lock(m); return data.empty(); } }; int main() { threadsafe_stack si; si.push(5); si.pop(); if(!si.empty()) { int x; si.pop(x); } }