採用上一篇所說的方案一和方案三定義的一個線程安全的stack,它其實是個stack的包裝類,代碼如下:
struct empty_stack : exception
{
const char* what() const throw()
{
return "the stack is empty";
};
};
template<typename T>
class threadsafe_stack
{
private:
stack<T> data;
mutable mutex m;
public:
threadsafe_stack() {}
threadsafe_stack(const threadsafe_stack& other)
{
lock_guard<mutex> lock(other.m);
data = other.data;
}
threadsafe_stack& operator=(const threadsafe_stack&) = delete;
void push(T new_value)
{
lock_guard<mutex> lock(m);
data.push(new_value);
}
shared_ptr<T> pop()
{
lock_guard<mutex> lock(m);
if (data.empty())
throw empty_stack();
shared_ptr<T> const res(make_shared<T>(data.top()));
data.pop();
return res;
}
void pop(T& value)
{
lock_guard<mutex> lock(m);
if (data.empty())
throw empty_stack();
value = data.top();
data.pop();
}
bool empty() const
{
lock_guard<mutex> lock(m);
return data.empty();
}
};
爲了最大化其安全性,整個stack的操作都被嚴格限制。賦值操作被刪除,但是可以拷貝構造,假設其元素類型支持拷貝。2個pop()函數有可能拋出empty_stack異常,保證即使stack爲空的情況下執行pop也沒有問題。接口由原來的5個變爲現在的3個:push()、pop()、empty(),儘管empty()有些多餘。
注意他的拷貝構造函數,他沒有在初始化列表中初始化成員,而是在函數體內賦值。這樣做的目的是爲了讓mutex起到保護作用。