码迷,mamicode.com
首页 > 其他好文 > 详细

RAII 封装的 thread

时间:2016-02-14 01:39:48      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:

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

标签:

原文地址:http://www.cnblogs.com/wuOverflow/p/5188516.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!