我有一個std :: thread成員的可移動的非拷貝類。如何測試std :: thread是否從?中移出?
當類析構函數運行時,我需要做一些清理工作並加入線程。 如果類從我需要的析構函數跳過清理和線程連接。 我可以通過存儲從一個布爾移動但這似乎有點浪費完成這個。
如果std :: thread成員從那裏移動,那麼我知道這個類實例是從中移出的。是否有可能檢查std :: thread成員是否被移出?
class Widget
{
Widget()
{
// initialize
}
Widget(Widget&& rhs)
{
t = std::move(rhs.t);
}
~Widget()
{
if (t_is_not_moved_from())
{
// do cleanup
t.join();
}
}
inline friend void swap(Widget& lhs, Widget& rhs)
{
lhs.t.swap(rhs.t);
}
private:
std::thread t;
// noncopyable
Widget(const Widget&);
const Widget& operator=(const Widget&);
};
您不必爲std :: thread編寫移動構造函數和析構函數。默認生成的將完成你想要做的事情。 – balki 2013-04-21 07:26:46