首页 > 其他 > 详细

RAII 封装的 thread

时间:2016-02-14 01:38:57      阅读:274      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!