#ifndef stack_h #define stack_h 1 #include #include template class stack { public: using empty = std::exception; stack(): head(nullptr) {} ~stack() { while (head) pop(); } void push(const T& t) { std::lock_guard l(m); head = new node{head, t}; } T pop() { std::lock_guard l(m); if (head != nullptr) { node *n = head; head = n->next; T t = n->data; delete n; return t; } throw empty(); } private: struct node { node* next; T data; }; std::mutex m; node* head; }; #endif // stack_h