举例,线程安全的栈#include <exception>
#include <memory>
#include <mutex>
#include <stack>
struct EmptyStack : std::exception
{
const char * what() const throw();
};
template<class T>
class ThreadSafeStack
{
private:
std::stack<T> data;
mutable std::mutex m;
public:
ThreadSafeStack(){}
ThreadSafeStack(const ThreadSafeStack & other)
{
std::lock_guard<std::mutex> lock(other.m);
data = other.data;
}
ThreadSafeStack & operator=(const ThreadSafeStack&) = delete;
void push(T newValue)
{
std::lock_guard<std::mutex> lock(m);
data.push(std::move(newValue));
}
std::shared_ptr<T> pop()
{
std::lock_guard<std::mutex> lock(m);
if(data.empty()) throw EmptyStack();
std::shared_ptr<T> const res(std::make_shared<T>(data.top()));
data.pop();
return res;
}
void pop(T& value)
{
std::lock_guard<std::mutex> lock(m);
if(data.empty()) throw EmptyStack();
value = data.top;
data.pop();
}
bool empty() const
{
std::lock_guard<std::mutex> lock(m);
return data.empty();
}
}