#include <boost/thread/thread.hpp> namespace boost { class thread; void swap(thread& lhs,thread& rhs) noexcept; namespace this_thread { thread::id get_id() noexcept; template<typename TimeDuration> void yield() noexcept; template <class Clock, class Duration> void sleep_until(const chrono::time_point<Clock, Duration>& abs_time); template <class Rep, class Period> void sleep_for(const chrono::duration<Rep, Period>& rel_time); namespace no_interruption_point // EXTENSION { template <class Clock, class Duration> void sleep_until(const chrono::time_point<Clock, Duration>& abs_time); template <class Rep, class Period> void sleep_for(const chrono::duration<Rep, Period>& rel_time); } template<typename Callable> void at_thread_exit(Callable func); // EXTENSION void interruption_point(); // EXTENSION bool interruption_requested() noexcept; // EXTENSION bool interruption_enabled() noexcept; // EXTENSION class disable_interruption; // EXTENSION class restore_interruption; // EXTENSION #if defined BOOST_THREAD_USES_DATETIME template <TimeDuration> void sleep(TimeDuration const& rel_time); // DEPRECATED void sleep(system_time const& abs_time); // DEPRECATED #endif } class thread_group; // EXTENSION }
boost::thread
类负责启动和管理线程。每个 boost::thread
对象代表一个单独的执行线程,或 非线程,并且最多一个 boost::thread
对象代表给定的执行线程:boost::thread
类型的对象不可复制。
然而,boost::thread
类型的对象是可移动的,因此它们可以存储在可移动容器中,并从函数返回。 这允许将线程创建的细节包装在函数中。
boost::thread make_thread(); void f() { boost::thread some_thread=make_thread(); some_thread.join(); }
![]() |
注意 |
---|---|
在支持右值引用的编译器上, 对于其他编译器,移动支持通过移动仿真层提供,因此容器必须显式检测该移动仿真层。 有关详细信息,请参阅 <boost/thread/detail/move.hpp>。 |
通过将一个可调用类型的对象(可以使用无参数调用)传递给构造函数来启动新线程。 然后将该对象复制到内部存储,并在新创建的执行线程上调用。 如果该对象不得(或不能)被复制,则可以使用 boost::ref
传入对函数对象的引用。 在这种情况下,Boost.Thread 的用户必须确保被引用的对象比新创建的执行线程寿命更长。
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
如果您希望使用需要提供参数的函数或可调用对象构造 boost::thread
的实例,则可以通过将其他参数传递给 boost::thread
构造函数来完成
void find_the_question(int the_answer); boost::thread deep_thought_2(find_the_question,42);
参数被复制到内部线程结构中:如果需要引用,请使用 boost::ref
,就像对可调用函数的引用一样。
可以传递的附加参数的数量没有明确的限制。
以这种方式启动的线程是使用实现定义的线程属性(如堆栈大小、调度、优先级等)或任何平台特定属性创建的。 如何提供允许用户设置平台特定属性的可移植接口尚不清楚。 Boost.Thread 采取折衷方案,通过类 thread::attributes 允许至少以可移植的方式设置堆栈大小,如下所示
boost::thread::attributes attrs; attrs.set_stack_size(4096*10); boost::thread deep_thought_2(attrs, find_the_question, 42);
即使对于这个简单的属性,也可能存在可移植性问题,因为某些平台可能要求堆栈大小应具有最小大小和/或是给定页面大小的倍数。 该库会根据平台约束调整请求的大小,以便用户无需担心。
这是以可移植方式提供的唯一属性。 为了在构造时设置任何其他线程属性,用户需要使用不可移植的代码。
在 PThread 平台上,用户将需要获取线程属性句柄并将其用于任何属性。
接下来介绍用户如何在 PThread 平台上设置堆栈大小和调度策略。
boost::thread::attributes attrs; // set portable attributes // ... attr.set_stack_size(4096*10); #if defined(BOOST_THREAD_PLATFORM_WIN32) // ... window version #elif defined(BOOST_THREAD_PLATFORM_PTHREAD) // ... pthread version pthread_attr_setschedpolicy(attr.native_handle(), SCHED_RR); #else #error "Boost threads unavailable on this platform" #endif boost::thread th(attrs, find_the_question, 42);
在 Windows 平台上,情况并非如此简单,因为没有编译线程属性的类型。 有一个与 Windows 上线程创建相关的类型,它通过 thread::attributes 类进行模拟。 这是 LPSECURITY_ATTRIBUTES lpThreadAttributes。 Boost.Thread 提供了一个不可移植的 set_security 函数,以便用户可以在线程创建之前提供它,如下所示
#if defined(BOOST_THREAD_PLATFORM_WIN32) boost::thread::attributes attrs; // set portable attributes attr.set_stack_size(4096*10); // set non portable attribute LPSECURITY_ATTRIBUTES sec; // init sec attr.set_security(sec); boost::thread th(attrs, find_the_question, 42); // Set other thread attributes using the native_handle_type. //... #else #error "Platform not supported" #endif
如果传递给 boost::thread
构造函数的函数或可调用对象在调用时传播了异常,但该异常不是 boost::thread_interrupted
类型,则会调用 std::terminate()
。
可以通过显式调用 detach()
成员函数在 boost::thread
对象上分离线程。 在这种情况下,boost::thread
对象不再代表现在分离的线程,而是代表 非线程。
int main() { boost::thread t(my_func); t.detach(); }
为了等待执行线程完成,必须使用 join()
、__join_for 或 __join_until(timed_join()
已弃用)boost::thread
对象的成员函数。join()
将阻塞调用线程,直到由 boost::thread
对象表示的线程完成。
int main() { boost::thread t(my_func); t.join(); }
如果由 boost::thread
对象表示的执行线程已完成,或者 boost::thread
对象代表 非线程,则 join()
立即返回。
int main() { boost::thread t; t.join(); // do nothing }
基于时间的加入类似,除了如果等待的线程在指定时间过去或分别到达时未完成,则对 __join_for 或 __join_until 的调用也会返回。
int main() { boost::thread t; if ( t.join_for(boost::chrono::milliseconds(500)) ) // do something else t.join(); // join anyway }
当代表执行线程的 boost::thread
对象被销毁时,线程将变为 分离状态。 一旦线程被分离,它将继续执行,直到构造时提供的函数或可调用对象的调用完成,或者程序终止。 线程也可以通过显式调用 detach()
成员函数在 boost::thread
对象上分离。 在这种情况下,boost::thread
对象不再代表现在分离的线程,而是代表 非线程。
当代表执行线程的 boost::thread
对象被销毁时,如果线程是 __joinable__,程序将终止。
int main() { boost::thread t(my_func); } // calls std::terminate()
您可以使用 thread_joiner 来确保线程在线程析构函数中已被加入。
int main() { boost::thread t(my_func); boost::thread_joiner g(t); // do something else } // here the thread_joiner destructor will join the thread before it is destroyed.
可以通过调用相应 boost::thread
对象的 interrupt()
成员函数来中断正在运行的线程。 当中断线程接下来执行指定的中断点之一(或者如果它当前在执行其中一个时被 阻塞),并且启用了中断,则将在中断线程中抛出 boost::thread_interrupted
异常。 除非在中断线程的线程主函数内部捕获此异常,否则堆栈展开过程(与任何其他异常一样)会导致执行具有自动存储持续时间的析构函数。 与其他异常不同,当 boost::thread_interrupted
从线程主函数中传播出来时,这不会导致调用 std::terminate
;效果就像线程主函数已正常返回一样。
如果线程希望避免被中断,它可以创建 boost::this_thread::disable_interruption
的实例。 此类的对象会在构造时为创建它们的线程禁用中断,并在销毁时将中断状态恢复为之前的状态
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 }
boost::this_thread::disable_interruption
实例的效果可以通过构造 boost::this_thread::restore_interruption
的实例来暂时反转,传入有问题的 boost::this_thread::disable_interruption
对象。 这会将中断状态恢复为构造 boost::this_thread::disable_interruption
对象时的状态,然后在销毁 boost::this_thread::restore_interruption
对象时再次禁用中断。
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 }
在任何时候,都可以通过调用 boost::this_thread::interruption_enabled()
来查询当前线程的中断状态。
以下函数是中断点,如果为当前线程启用中断,并且为当前线程请求中断,则它们将抛出 boost::thread_interrupted
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()
boost::thread::id
类的对象可用于标识线程。 每个正在运行的执行线程都有一个唯一的 ID,可以从相应的 boost::thread
通过调用 get_id()
成员函数获得,或者通过从线程内部调用 boost::this_thread::get_id()
获得。boost::thread::id
类的对象可以复制,并用作关联容器中的键:提供了全范围的比较运算符。 线程 ID 也可以使用流插入运算符写入输出流,但输出格式未指定。
boost::thread::id
的每个实例要么引用某个线程,要么引用 非线程。 引用 非线程 的实例彼此比较相等,但不等于引用实际执行线程的任何实例。 boost::thread::id
上的比较运算符为每个不相等的线程 ID 产生一个全序。
boost::thread
类具有成员 native_handle_type
和 native_handle
,提供对底层原生句柄的访问。
此原生句柄可用于更改例如调度。
通常,将此句柄与可能与 Boost.Thread 提供的操作冲突的操作一起使用是不安全的。 一个错误用法的示例可能是直接分离线程,因为它不会更改 boost::thread
实例的内部结构,因此例如 joinable 函数将继续返回 true,而原生线程不再可加入。
thread t(fct); thread::native_handle_type hnd=t.native_handle(); pthread_detach(hnd); assert(t.joinable());
使用原生接口创建的任何执行线程在本文档中都称为原生线程。
原生执行线程的第一个示例是主线程。
用户可以使用 boost::this_thread
yield
、sleep
、sleep_for
、sleep_until
函数访问与原生当前线程相关的一些同步函数。
int main() { // ... boost::this_thread::sleep_for(boost::chrono::milliseconds(10)); // ... }
当然,Boost.Thread 提供的所有同步工具也可在原生线程上使用。
当从使用原生接口创建的线程调用时,boost::this_thread
中断相关函数以降级模式运行,即 boost::this_thread::interruption_enabled()
返回 false。 因此,使用 boost::this_thread::disable_interruption
和 boost::this_thread::restore_interruption
将不起作用,并且对 boost::this_thread::interruption_point()
的调用将被忽略。
由于中断线程的唯一方法是通过 boost::thread
实例,因此 interruption_request()
对于原生线程将返回 false。
pthread_exit
POSIX 限制
glibc/NPTL 中的 pthread_exit
会导致“强制展开”,这几乎类似于 C++ 异常,但又不完全是。 例如,在 Mac OS X 上,pthread_exit
会在不调用 C++ 析构函数的情况下展开。
此行为与当前的 Boost.Thread 设计不兼容,因此在 POSIX 线程中使用此函数会导致任何 Boost.Thread 函数的未定义行为。
joinable()
join()
timed_join()
已弃用try_join_for()
扩展try_join_until()
扩展detach()
get_id()
interrupt()
扩展hardware_concurrency()
physical_concurrency()
native_handle()
operator==
已弃用operator!=
已弃用sleep()
已弃用yield()
已弃用swap()
swap()
boost::thread::id
boost::thread::attributes
扩展#include <boost/thread/thread.hpp> class thread { public: class attributes; // EXTENSION thread() noexcept; ~thread(); thread(const thread&) = delete; thread& operator=(const thread&) = delete; // move support thread(thread&&) noexcept; thread& operator=(thread&&) noexcept; template <class F> explicit thread(F f); template <class F> thread(F &&f); template <class F,class A1,class A2,...> thread(F f,A1 a1,A2 a2,...); template <class F, class ...Args> explicit thread(F&& f, Args&&... args); template <class F> explicit thread(attributes& attrs, F f); // EXTENSION template <class F> thread(attributes& attrs, F &&f); // EXTENSION template <class F, class ...Args> explicit thread(attributes& attrs, F&& f, Args&&... args); void swap(thread& x) noexcept; class id; id get_id() const noexcept; bool joinable() const noexcept; void join(); template <class Rep, class Period> bool try_join_for(const chrono::duration<Rep, Period>& rel_time); // EXTENSION template <class Clock, class Duration> bool try_join_until(const chrono::time_point<Clock, Duration>& t); // EXTENSION void detach(); static unsigned hardware_concurrency() noexcept; static unsigned physical_concurrency() noexcept; typedef platform-specific-type native_handle_type; native_handle_type native_handle(); void interrupt(); // EXTENSION bool interruption_requested() const noexcept; // EXTENSION #if defined BOOST_THREAD_USES_DATETIME bool timed_join(const system_time& wait_until); // DEPRECATED template<typename TimeDuration> bool timed_join(TimeDuration const& rel_time); // DEPRECATED static void sleep(const system_time& xt);// DEPRECATED #endif #if defined BOOST_THREAD_PROVIDES_THREAD_EQ bool operator==(const thread& other) const; // DEPRECATED bool operator!=(const thread& other) const; // DEPRECATED #endif static void yield() noexcept; // DEPRECATED }; void swap(thread& lhs,thread& rhs) noexcept;
thread() noexcept;
构造一个 boost::thread
实例,该实例引用 非线程。
this->get_id()==thread::id()
无
thread(thread&& other) noexcept;
将由 other
管理的线程的所有权(如果有)转移到新构造的 boost::thread
实例。
other.get_id()==thread::id()
并且 get_id()
返回构造之前 other.get_id()
的值
无
thread& operator=(thread&& other) noexcept;
将由 other
管理的线程的所有权(如果有)转移到 *this
。
- 如果定义了 BOOST_THREAD_DONT_PROVIDE_THREAD_MOVE_ASSIGN_CALLS_TERMINATE_IF_JOINABLE:如果线程是可连接的,则调用 detach()
,已弃用
- 如果定义了 BOOST_THREAD_PROVIDES_THREAD_MOVE_ASSIGN_CALLS_TERMINATE_IF_JOINABLE:如果线程是可连接的,则调用 std::terminate()
。
other->get_id()==thread::id()
并且 get_id()
返回赋值之前 other.get_id()
的值。
无
template<typename Callable> thread(Callable func);
Callable
必须是可复制的,并且 func()
必须是有效的表达式。
func
被复制到由线程库内部管理的存储中,并且该副本在新创建的执行线程上被调用。如果此调用导致异常传播到线程库的内部,且该异常不是 boost::thread_interrupted
类型,则将调用 std::terminate()
。来自此调用的任何返回值都将被忽略。
*this
引用新创建的执行线程,并且 this->get_id()!=thread::id()
。
如果发生错误,则抛出 boost::thread_resource_error
。
resource_unavailable_try_again : 系统缺乏创建另一个线程的必要资源,或者系统施加的进程中线程数量限制将被超出。
template<typename Callable> thread(attributes& attrs, Callable func);
Callable
必须是可复制的。
func
被复制到由线程库内部管理的存储中,并且该副本在新创建的执行线程上使用指定的属性被调用。如果此调用导致异常传播到线程库的内部,且该异常不是 boost::thread_interrupted
类型,则将调用 std::terminate()
。来自此调用的任何返回值都将被忽略。如果属性声明原生线程为 detached,则 boost::thread 将被 detached。
*this
引用新创建的执行线程,并且 this->get_id()!=thread::id()
。
如果发生错误,则抛出 boost::thread_resource_error
。
resource_unavailable_try_again : 系统缺乏创建另一个线程的必要资源,或者系统施加的进程中线程数量限制将被超出。
template<typename Callable> thread(Callable &&func);
Callable
必须是可移动的。
func
被移动到由线程库内部管理的存储中,并且该副本在新创建的执行线程上被调用。如果此调用导致异常传播到线程库的内部,且该异常不是 boost::thread_interrupted
类型,则将调用 std::terminate()
。来自此调用的任何返回值都将被忽略。
*this
引用新创建的执行线程,并且 this->get_id()!=thread::id()
。
如果发生错误,则抛出 boost::thread_resource_error
。
resource_unavailable_try_again : 系统缺乏创建另一个线程的必要资源,或者系统施加的进程中线程数量限制将被超出。
template<typename Callable> thread(attributes& attrs, Callable func);
Callable
必须是可复制的。
func
被复制到由线程库内部管理的存储中,并且该副本在新创建的执行线程上使用指定的属性被调用。如果此调用导致异常传播到线程库的内部,且该异常不是 boost::thread_interrupted
类型,则将调用 std::terminate()
。来自此调用的任何返回值都将被忽略。如果属性声明原生线程为 detached,则 boost::thread 将被 detached。
*this
引用新创建的执行线程,并且 this->get_id()!=thread::id()
。
如果发生错误,则抛出 boost::thread_resource_error
。
resource_unavailable_try_again : 系统缺乏创建另一个线程的必要资源,或者系统施加的进程中线程数量限制将被超出。
template <class F,class A1,class A2,...> thread(F f,A1 a1,A2 a2,...);
F
和每个 A
n 必须是可复制或可移动的。
如同 thread(boost::bind(f,a1,a2,...))
。因此,f
和每个 a
n 都被复制到内部存储中,以供新线程访问。
*this
引用新创建的执行线程。
如果发生错误,则抛出 boost::thread_resource_error
。
resource_unavailable_try_again : 系统缺乏创建另一个线程的必要资源,或者系统施加的进程中线程数量限制将被超出。
目前,除了函数 f
之外,还可以指定最多九个附加参数 a1
到 a9
。
~thread();
- 如果定义了 BOOST_THREAD_DONT_PROVIDE_THREAD_DESTRUCTOR_CALLS_TERMINATE_IF_JOINABLE:如果线程是可连接的,则调用 detach()
,已弃用
- 如果定义了 BOOST_THREAD_PROVIDES_THREAD_DESTRUCTOR_CALLS_TERMINATE_IF_JOINABLE:如果线程是可连接的,则调用 std::terminate
。销毁 *this
。
无。
移动到 std::terminate 的原因是,在其析构函数中隐式地 detached 或 join 一个 joinable()
线程可能会导致难以调试的正确性错误(对于 detach
)或性能错误(对于 join
),这些错误仅在引发异常时才会遇到。因此,程序员必须确保在线程仍然是可连接状态时,永远不会执行析构函数。在销毁之前 Join 线程或使用作用域线程。
void join();
线程是可连接的。
如果 *this
引用一个执行线程,则等待该执行线程完成。
由 *this
代表的线程的完成与相应的成功 join()
返回同步。
对 *this 的操作不同步。
如果 *this
在进入时引用一个执行线程,则该执行线程已完成。*this
不再引用任何执行线程。
如果当前执行线程被中断或 system_error
,则抛出 boost::thread_interrupted
resource_deadlock_would_occur: 如果检测到死锁,或者 this->get_id() == boost::this_thread::get_id()
。
invalid_argument: 如果线程不可连接并且定义了 BOOST_THREAD_THROW_IF_PRECONDITION_NOT_SATISFIED
。
join()
是预定义的 中断点 之一。
bool timed_join(const system_time& wait_until); template<typename TimeDuration> bool timed_join(TimeDuration const& rel_time);
![]() |
警告 |
---|---|
自 3.00 版本起已弃用。 |
线程是可连接的。
如果 *this
引用一个执行线程,则等待该执行线程完成,直到达到时间 wait_until
或指定的持续时间 rel_time
过去。如果 *this
不引用执行线程,则立即返回。
如果 *this
在进入时引用一个执行线程,并且该执行线程在调用超时之前已完成,则返回 true
,否则返回 false
。
如果 *this
在进入时引用一个执行线程,并且 timed_join
返回 true
,则该执行线程已完成,并且 *this
不再引用任何执行线程。如果此 timed_join
调用返回 false
,则 *this
保持不变。
如果当前执行线程被中断或 system_error
,则抛出 boost::thread_interrupted
resource_deadlock_would_occur: 如果检测到死锁,或者 this->get_id() == boost::this_thread::get_id()。
invalid_argument: 如果线程不可连接并且定义了 BOOST_THREAD_THROW_IF_PRECONDITION_NOT_SATISFIED。
timed_join()
是预定义的 中断点 之一。
template <class Rep, class Period> bool try_join_for(const chrono::duration<Rep, Period>& rel_time);
线程是可连接的。
如果 *this
引用一个执行线程,则等待该执行线程完成,直到指定的持续时间 rel_time
过去。如果 *this
不引用执行线程,则立即返回。
如果 *this
在进入时引用一个执行线程,并且该执行线程在调用超时之前已完成,则返回 true
,否则返回 false
。
如果 *this
在进入时引用一个执行线程,并且 try_join_for
返回 true
,则该执行线程已完成,并且 *this
不再引用任何执行线程。如果此 try_join_for
调用返回 false
,则 *this
保持不变。
如果当前执行线程被中断或 system_error
,则抛出 boost::thread_interrupted
resource_deadlock_would_occur: 如果检测到死锁,或者 this->get_id() == boost::this_thread::get_id()。
invalid_argument: 如果线程不可连接并且定义了 BOOST_THREAD_THROW_IF_PRECONDITION_NOT_SATISFIED。
try_join_for()
是预定义的 中断点 之一。
template <class Clock, class Duration> bool try_join_until(const chrono::time_point<Clock, Duration>& abs_time);
线程是可连接的。
如果 *this
引用一个执行线程,则等待该执行线程完成,直到达到时间 abs_time
。如果 *this
不引用执行线程,则立即返回。
如果 *this
在进入时引用一个执行线程,并且该执行线程在调用超时之前已完成,则返回 true
,否则返回 false
。
如果 *this
在进入时引用一个执行线程,并且 try_join_until
返回 true
,则该执行线程已完成,并且 *this
不再引用任何执行线程。如果此 try_join_until
调用返回 false
,则 *this
保持不变。
如果当前执行线程被中断或 system_error
,则抛出 boost::thread_interrupted
resource_deadlock_would_occur: 如果检测到死锁,或者 this->get_id() == boost::this_thread::get_id()。
invalid_argument: 如果线程不可连接并且定义了 BOOST_THREAD_THROW_IF_PRECONDITION_NOT_SATISFIED。
try_join_until()
是预定义的 中断点 之一。
void detach();
线程是可连接的。
执行线程变为 detached,并且不再具有关联的 boost::thread
对象。
*this
不再引用任何执行线程。
system_error
no_such_process: 如果线程无效。
invalid_argument: 如果线程不可连接并且定义了 BOOST_THREAD_THROW_IF_PRECONDITION_NOT_SATISFIED。
thread::id get_id() const noexcept;
如果 *this
引用一个执行线程,则返回一个 boost::thread::id
实例,该实例表示该线程。否则,返回一个默认构造的 boost::thread::id
。
无
void interrupt();
unsigned hardware_concurrency() noexcept;
当前系统上可用的硬件线程数(例如,CPU 或核心或超线程单元的数量),如果此信息不可用,则为 0。
无
unsigned physical_concurrency() noexcept;
当前系统上可用的物理核心数。与 hardware_concurrency()
相比,它不返回虚拟核心的数量,而仅计数物理核心。
无
typedef platform-specific-type native_handle_type; native_handle_type native_handle();
返回 native_handle_type
的实例,该实例可以与平台特定的 API 一起使用,以操作底层实现。如果不存在此类实例,则 native_handle()
和 native_handle_type
不存在。
无。
bool operator==(const thread& other) const;
get_id()==other.get_id()
bool operator!=(const thread& other) const;
get_id()!=other.get_id()
void sleep(system_time const& abs_time);
![]() |
警告 |
---|---|
自 3.0.0 版本起已弃用。 请改用 |
暂停当前线程,直到达到指定时间。
如果当前执行线程被中断,则抛出 boost::thread_interrupted
。
sleep()
是预定义的 中断点 之一。
void swap(thread& other) noexcept;
交换与 *this
和 other
关联的执行线程,因此 *this
与调用之前与 other
关联的执行线程关联,反之亦然。
this->get_id()
返回与调用之前 other.get_id()
相同的值。other.get_id()
返回与调用之前 this->get_id()
相同的值。
无。
#include <boost/thread/thread.hpp> void swap(thread& lhs,thread& rhs) noexcept;
#include <boost/thread/thread.hpp> class thread::id { public: id() noexcept; bool operator==(const id& y) const noexcept; bool operator!=(const id& y) const noexcept; bool operator<(const id& y) const noexcept; bool operator>(const id& y) const noexcept; bool operator<=(const id& y) const noexcept; bool operator>=(const id& y) const noexcept; template<class charT, class traits> friend std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& os, const id& x); };
bool operator==(const id& y) const noexcept;
如果 *this
和 y
都表示相同的执行线程,或者都表示 非线程,则返回 true
,否则返回 false
。
无
bool operator!=(const id& y) const noexcept;
如果 *this
和 y
表示不同的执行线程,或者一个表示执行线程,而另一个表示 非线程,则返回 true
,否则返回 false
。
无
bool operator<(const id& y) const noexcept;
如果 *this!=y
为 true
,并且 boost::thread::id
值的实现定义的全序关系将 *this
放在 y
之前,则为 true
,否则为 false
。
无
一个表示 非线程 的 boost::thread::id
实例将始终小于表示执行线程的实例。
template<class charT, class traits> friend std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& os, const id& x);
将 boost::thread::id
实例 x
的表示形式写入流 os
,使得如果 a==b
,则两个 boost::thread::id
实例 a
和 b
的表示形式相同;如果 a!=b
,则表示形式不同。
os
class thread::attributes { public: attributes() noexcept; ~ attributes()=default; // stack void set_stack_size(std::size_t size) noexcept; std::size_t get_stack_size() const noexcept; #if defined BOOST_THREAD_DEFINES_THREAD_ATTRIBUTES_NATIVE_HANDLE typedef platform-specific-type native_handle_type; native_handle_type* native_handle() noexcept; const native_handle_type* native_handle() const noexcept; #endif };
void set_stack_size(std::size_t size) noexcept;
存储用于创建线程的堆栈大小。这是一个提示,表明如果堆栈太小、太大或未与页对齐,则实现可以选择更好的大小。
this-> get_stack_size()
返回所选择的堆栈大小。
无。
std::size_t get_stack_size() const noexcept;
在创建线程时要使用的堆栈大小。请注意,此函数可以返回 0,表示默认值。
无。
typedef platform-specific-type native_handle_type; typedef platform-specific-type native_handle_type; native_handle_type* native_handle() noexcept; const native_handle_type* native_handle() const noexcept;
返回一个 native_handle_type
实例,该实例可以与平台特定的 API 一起使用,以操作底层线程属性实现。如果不存在这样的实例,则 native_handle()
和 native_handle_type
不存在,并且未定义 BOOST_THREAD_DEFINES_THREAD_ATTRIBUTES_NATIVE_HANDLE
。
无。
get_id()
interruption_point()
EXTENSIONinterruption_requested()
EXTENSIONinterruption_enabled()
EXTENSIONsleep()
DEPRECATEDsleep_until()
sleep_for()
yield()
disable_interruption
EXTENSIONrestore_interruption
EXTENSIONat_thread_exit()
EXTENSIONnamespace boost { namespace this_thread { thread::id get_id() noexcept; template<typename TimeDuration> void yield() noexcept; template <class Clock, class Duration> void sleep_until(const chrono::time_point<Clock, Duration>& abs_time); template <class Rep, class Period> void sleep_for(const chrono::duration<Rep, Period>& rel_time); template<typename Callable> void at_thread_exit(Callable func); // EXTENSION void interruption_point(); // EXTENSION bool interruption_requested() noexcept; // EXTENSION bool interruption_enabled() noexcept; // EXTENSION class disable_interruption; // EXTENSION class restore_interruption; // EXTENSION #if defined BOOST_THREAD_USES_DATETIME void sleep(TimeDuration const& rel_time); // DEPRECATED void sleep(system_time const& abs_time); // DEPRECATED #endif } }
#include <boost/thread/thread.hpp> namespace this_thread { thread::id get_id() noexcept; }
一个 boost::thread::id
实例,表示当前正在执行的线程。
如果发生错误,则抛出 boost::thread_resource_error
。
#include <boost/thread/thread.hpp> namespace this_thread { void interruption_point(); }
检查当前线程是否已被中断。
如果 boost::this_thread::interruption_enabled()
和 boost::this_thread::interruption_requested()
都返回 true
,则抛出 boost::thread_interrupted
异常。
#include <boost/thread/thread.hpp> namespace this_thread { bool interruption_requested() noexcept; }
如果已为当前线程请求中断,则为 true
,否则为 false
。
无。
#include <boost/thread/thread.hpp> namespace this_thread { bool interruption_enabled() noexcept; }
如果已为当前线程启用中断,则为 true
,否则为 false
。
无。
#include <boost/thread/thread.hpp> namespace this_thread { template<typename TimeDuration> void sleep(TimeDuration const& rel_time); void sleep(system_time const& abs_time) }
![]() |
警告 |
---|---|
自 3.0.0 版本起已弃用。 请改用 |
暂停当前线程,直到由 rel_time
指定的时间段过去,或者到达由 abs_time
指定的时间点。
如果当前执行线程被中断,则抛出 boost::thread_interrupted
。
sleep()
是预定义的 中断点 之一。
#include <boost/thread/thread.hpp> namespace this_thread { template <class Clock, class Duration> void sleep_until(const chrono::time_point<Clock, Duration>& abs_time); namespace no_interruption_point { template <class Clock, class Duration> void sleep_until(const chrono::time_point<Clock, Duration>& abs_time); } }
暂停当前线程,直到到达由 abs_time
指定的时间点。
如果 Clock 满足 TrivialClock 要求且 Duration 的操作不抛出异常,则无任何操作。如果当前执行线程被中断,则抛出 boost::thread_interrupted
异常。
sleep_until()
是预定义的 中断点 之一。
no_interruption_point::sleep_until()
不是 中断点 之一。
#include <boost/thread/thread.hpp> namespace this_thread { template <class Rep, class Period> void sleep_for(const chrono::duration<Rep, Period>& rel_time); namespace no_interruption_point { template <class Rep, class Period> void sleep_for(const chrono::duration<Rep, Period>& rel_time); } }
暂停当前线程,直到由 rel_time
指定的持续时间过去。
如果 chrono::duration<Rep, Period>
的操作不抛出异常,则无任何操作。如果当前执行线程被中断,则抛出 boost::thread_interrupted
异常。
sleep_for()
是预定义的 中断点 之一。
no_interruption_point:: sleep_for()
不是 中断点 之一。
#include <boost/thread/thread.hpp> namespace this_thread { void yield() noexcept; }
放弃当前线程剩余的时间片,以允许其他线程运行。
无。
#include <boost/thread/thread.hpp> namespace this_thread { class disable_interruption { public: disable_interruption(const disable_interruption&) = delete; disable_interruption& operator=(const disable_interruption&) = delete; disable_interruption() noexcept; ~disable_interruption() noexcept; }; }
boost::this_thread::disable_interruption
在构造时禁用当前线程的中断,并在析构时恢复先前的中断状态。disable_interruption
的实例无法复制或移动。
disable_interruption() noexcept;
存储 boost::this_thread::interruption_enabled()
的当前状态,并禁用当前线程的中断。
对于当前线程,boost::this_thread::interruption_enabled()
返回 false
。
无。
~disable_interruption() noexcept;
必须从构造 *this
的同一线程调用。
将当前线程的 boost::this_thread::interruption_enabled()
状态恢复到构造 *this
之前的状态。
当前线程的 boost::this_thread::interruption_enabled()
返回在 *this
的构造函数中存储的值。
无。
#include <boost/thread/thread.hpp> namespace this_thread { class restore_interruption { public: restore_interruption(const restore_interruption&) = delete; restore_interruption& operator=(const restore_interruption&) = delete; explicit restore_interruption(disable_interruption& disabler) noexcept; ~restore_interruption() noexcept; }; }
在构造 boost::this_thread::restore_interruption
的实例时,当前线程的中断状态将恢复为提供的 disable_interruption
实例的构造函数存储的中断状态。当该实例被销毁时,中断将再次被禁用。restore_interruption
的实例无法复制或移动。
explicit restore_interruption(disable_interruption& disabler) noexcept;
必须从构造 disabler
的同一线程调用。
将当前线程的 boost::this_thread::interruption_enabled()
状态恢复到构造 disabler
之前的状态。
当前线程的 boost::this_thread::interruption_enabled()
返回在 disabler
的构造函数中存储的值。
无。
~restore_interruption() noexcept;
必须从构造 *this
的同一线程调用。
禁用当前线程的中断。
对于当前线程,boost::this_thread::interruption_enabled()
返回 false
。
无。
#include <boost/thread/thread.hpp> template<typename Callable> void at_thread_exit(Callable func);
func
的副本被放置在线程特定的存储中。当当前线程退出时,将调用此副本(即使线程已被中断)。
func
的副本已保存,以便在线程退出时调用。
如果无法为函数副本分配内存,则抛出 std::bad_alloc
异常;如果线程库中发生任何其他错误,则抛出 boost::thread_resource_error
异常。在将 func
复制到内部存储时抛出的任何异常。
如果线程通过平台特定的 API 被强制终止,或者如果线程由于调用 exit()
、abort()
或 std::terminate()
而终止,则 不会 调用此函数。 特别是,从 main()
返回等同于调用 exit()
,因此不会调用使用 at_thread_exit()
注册的任何函数
#include <boost/thread/thread.hpp> class thread_group { public: thread_group(const thread_group&) = delete; thread_group& operator=(const thread_group&) = delete; thread_group(); ~thread_group(); template<typename F> thread* create_thread(F threadfunc); void add_thread(thread* thrd); void remove_thread(thread* thrd); bool is_this_thread_in(); bool is_thread_in(thread* thrd); void join_all(); void interrupt_all(); int size() const; };
thread_group
提供了一个以某种方式相关的线程集合。可以使用 add_thread
和 create_thread
成员函数将新线程添加到组中。thread_group
不可复制或移动。
template<typename F> thread* create_thread(F threadfunc);
创建一个新的 boost::thread
对象,如同通过 new thread(threadfunc)
一样,并将其添加到组中。
this->size()
增加一,新线程正在运行。
指向新的 boost::thread
对象的指针。
void add_thread(thread* thrd);
表达式 delete thrd
是良构的,并且不会导致未定义的行为,并且 is_thread_in(thrd) == false
。
获取由 thrd
指向的 boost::thread
对象的所有权,并将其添加到组中。
this->size()
增加一。
void remove_thread(thread* thrd);
如果 thrd
是组的成员,则将其移除,而不调用 delete
。
如果 thrd
曾是组的成员,则 this->size()
减少一。
void join_all();
.
is_this_thread_in() == false
在组中的每个 boost::thread
对象上调用 join()
。
组中的每个线程都已终止。
由于 join()
是预定义的 中断点 之一,因此 join_all()
也是一个中断点。
bool is_this_thread_in();
如果组中存在线程 th
,使得 th.get_id() == this_thread::get_id()
,则为 true。
bool is_thread_in(thread* thrd);
如果组中存在线程 th
,使得 th.get_id() == thrd->get_id()
,则为 true。