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()
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()