Boost Thread 介绍

Each boost::thread object represents a single thread of execution, or Not-a-Thread。
objects of type boost::thread are not copyable.




/*****************Launching threads****************/


struct callable
{
    void operator()();
};


boost::thread copies_are_safe()
{
    callable x;
    return boost::thread(x);
} // x is destroyed, but the newly-created thread has a copy, so this is OK


boost::thread oops()
{
    callable x;
    return boost::thread(boost::ref(x));
} // x is destroyed, but the newly-created thread still has a reference
  // this leads to undefined behaviour




void find_the_question(int the_answer);


boost::thread deep_thought_2(find_the_question,42);//There is an unspecified limit on the number of additional arguments that can be passed.






/*****************Detaching thread****************/


A thread can be detached by explicitly invoking the detach() member function on the boost::thread object. In this case, the boost::thread object ceases to represent the now-detached thread, and instead represents Not-a-Thread.
When the boost::thread object that represents a thread of execution is destroyed the thread becomes detached. Once a thread is detached, it will continue executing until the invocation of the function or callable object supplied on construction has completed, or the program is terminated. 


/************Joining a thread*************/


In order to wait for a thread of execution to finish, the join(), __join_for or __join_until ( timed_join() deprecated) member functions of the boost::thread object must be used. join() will block the calling thread until the thread represented by the boost::thread object has completed.




/***********Interruption*************/


A running thread can be interrupted by invoking the interrupt() member function of the corresponding boost::thread object,When the interrupted thread  executes one of the specified interruption points with interruption enabled.
Then a boost::thread_interrupted exception will be thrown in the interrupted thread.Unlike other exceptions, when boost::thread_interrupted is propagated out of thread-main function, this does not cause the call to std::terminate; the effect is as though the thread-main function has returned normally.


If a thread wishes to avoid being interrupted, it can create an instance of boost::this_thread::disable_interruption. Objects of this class disable interruption for the thread that created them on construction, and restore the interruption state to whatever it was before on construction.
void f()
{
    // interruption enabled here
    {
        boost::this_thread::disable_interruption di;
        // interruption disabled
        {
            boost::this_thread::disable_interruption di2;
            // interruption still disabled
        } // di2 destroyed, interruption state restored
        // interruption still disabled
    } // di destroyed, interruption state restored
    // interruption now enabled
}




The effects of an instance of boost::this_thread::disable_interruption can be temporarily reversed(推翻) by constructing an instance of boost::this_thread::restore_interruption, passing in the boost::this_thread::disable_interruption object in question. This will restore the interruption state to what it was when the boost::this_thread::disable_interruption object was constructed, and then disable interruption again when the boost::this_thread::restore_interruption object is destroyed.


void g()
{
    // interruption enabled here
    {
        boost::this_thread::disable_interruption di;
        // interruption disabled
        {
            boost::this_thread::restore_interruption ri(di);
            // interruption now enabled
        } // ri destroyed, interruption disable again
    } // di destroyed, interruption state restored
    // interruption now enabled
}


the interruption state for the current thread can be queried by calling boost::this_thread:: bool interruption_enabled()


/*********Predefined Interruption Points *********/


boost::thread::join()
boost::thread::timed_join()
boost::thread::try_join_for(),
boost::thread::try_join_until(),


boost::condition_variable::wait()
boost::condition_variable::timed_wait()
boost::condition_variable::wait_for()
boost::condition_variable::wait_until()


boost::condition_variable_any::wait()
boost::condition_variable_any::timed_wait()
boost::condition_variable_any::wait_for()
boost::condition_variable_any::wait_until()


boost::thread::sleep()
boost::this_thread::sleep_for()
boost::this_thread::sleep_until()
boost::this_thread::interruption_point()































  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值