RAII 封装的 thread
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了RAII 封装的 thread相关的知识,希望对你有一定的参考价值。
class ThreadRAII
{
public:
// whether join or detach should be called,
// when a this object is destroyed.
enum class DtorAction { join, detach };
ThreadRAII(std::thread&& t, DtorAction a)
: action_(a), t_(std::move(t)) {}
ThreadRAII (ThreadRAII&&) = default;
ThreadRAII& operator=(ThreadRAII&&) = default;
~ThreadRAII()
{
if (t_.joinable()) {
if (action_ == DtorAction::join) {
t_.join();
} else {
t_.detach();
}
}
}
std::thread& get() { return t_; }
private:
DtorAction action_;
std::thread t_;
};
以上是关于RAII 封装的 thread的主要内容,如果未能解决你的问题,请参考以下文章