#include #include #include #include #include #include template class stack { private: std::stack data; mutable std::mutex m; public: stack() {} stack(const stack& other) { std::lock_guard lock(other.m); data = other.data; } stack& operator=(const 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 std::underflow_error("empty stack"); std::shared_ptr const res(new T(data.top())); data.pop(); return res; } void pop(T& value) { std::lock_guard lock(m); if (data.empty()) throw std::underflow_error("empty stack"); value = data.top(); data.pop(); } bool empty() const { std::lock_guard lock(m); return data.empty(); } }; int main() { stack s; s.push(1); std::cout << *s.pop() << std::endl; }