类
thread
表示 单个执行线程 。线程允许多个函数并发执行。紧接着关联的线程对象构造,线程开始执行(为任何 OS 调度延迟悬挂),始于作为 构造函数参数 提供的顶层函数。忽略顶层函数的返回值,而且若它以抛异常终止,则调用 std::terminate 。顶层函数可以通过 std::promise 或修改共享变量(这可能要求同步,见 std::mutex 与 std::atomic )交流其返回值或异常到调用方。std::thread
对象亦可在不表示任何线程的状态(默认构造、被移动、 detach 或 join 后),而执行线程可以不关联到任何thread
对象( detach 后)。没有两个std::thread
对象会表示同一执行线程;std::thread
不 可复制构造 (CopyConstructible) 或 可复制赋值 (CopyAssignable) ,尽管它 可移动构造 (MoveConstructible) 且 可移动赋值 (MoveAssignable) 。
成员类
表示线程的 id (公开成员类) |
成员函数
构造新的 thread 对象 (公开成员函数) | |
析构 thread 对象,必须合并或分离底层线程 (公开成员函数) | |
移动 thread 对象 (公开成员函数) | |
观察器 | |
检查线程是否可合并,即潜在地运行于平行环境中 (公开成员函数) | |
返回线程的 id (公开成员函数) | |
返回底层实现定义的线程句柄 (公开成员函数) | |
[静态] | 返回实现支持的并发线程数 (公开静态成员函数) |
操作 | |
等待线程完成其执行 (公开成员函数) | |
容许线程从线程句柄独立开来执行 (公开成员函数) | |
交换二个 thread 对象 (公开成员函数) |
非成员函数
(C++11) | 特化 std::swap 算法 (函数模板) |
std::thread::id
std::thread::id
定义于头文件 | ||
class thread::id; | (C++11 起) |
类 thread::id
是轻量的可频繁复制类,它作为 std::thread 对象的唯一标识符工作。此类的实例亦可保有不表示任何线程的特殊辨别值。一旦线程结束,则 std::thread::id
的值可为另一线程复用。此类为用作包括有序和无序的关联容器的关键而设计。
std::thread::join
void join(); | (C++11 起) |
阻塞当前线程,直至 *this 所标识的线程完成其执行。*this 所标识的线程的完成 同步于 从 join() 的成功返回。
参数
(无)
返回值
(无)
后置条件
joinable 为 false
异常
若错误发生则为 std::system_error 。
错误条件
- 若 this->get_id() == std::this_thread::get_id () (检测到死锁)则为 resource_deadlock_would_occur
- 若线程非法则为 no_such_process
- 若 joinable 为 false 则为 invalid_argument
示例
#include <iostream>
#include <thread>
#include <chrono>
void foo()
{
// 模拟昂贵操作
std::this_thread::sleep_for(std::chrono::seconds(1));
}
void bar()
{
// 模拟昂贵操作
std::this_thread::sleep_for(std::chrono::seconds(1));
}
int main()
{
std::cout << "starting first helper...\n";
std::thread helper1(foo);
std::cout << "starting second helper...\n";
std::thread helper2(bar);
std::cout << "waiting for helpers to finish..." << std::endl;
helper1.join();
helper2.join();
std::cout << "done!\n";
}
输出:
starting first helper...
starting second helper...
waiting for helpers to finish...
done!
std::thread::detach
void detach(); | (C++11 起) |
从 thread 对象分离执行的线程,允许执行独立地持续。 一旦线程退出,则释放所有分配的资源。 调用 detach
后, *this 不再占有任何线程。
参数
(无)
返回值
(无)
后置条件
joinable 为 false
异常
若 joinable() == false 或错误发生时为 std::system_error 。
示例
#include <iostream>
#include <chrono>
#include <thread>
void independentThread()
{
std::cout << "Starting concurrent thread.\n";
std::this_thread::sleep_for(std::chrono::seconds(2));
std::cout << "Exiting concurrent thread.\n";
}
void threadCaller()
{
std::cout << "Starting thread caller.\n";
std::thread t(independentThread);
t.detach();
std::this_thread::sleep_for(std::chrono::seconds(1));
std::cout << "Exiting thread caller.\n";
}
int main()
{
threadCaller();
std::this_thread::sleep_for(std::chrono::seconds(5));
}
可能的输出:
Starting thread caller.
Starting concurrent thread.
Exiting thread caller.
Exiting concurrent thread.
std::thread::swap
void swap( thread& other ) noexcept; | (C++11 起) |
互换二个 thread 对象的底层句柄。
参数
other - 要与之交换的 thread 返回值
(无)
示例
#include <iostream>
#include <thread>
#include <chrono>
void foo()
{
std::this_thread::sleep_for(std::chrono::seconds(1));
}
void bar()
{
std::this_thread::sleep_for(std::chrono::seconds(1));
}
int main()
{
std::thread t1(foo);
std::thread t2(bar);
std::cout << "thread 1 id: " << t1.get_id() << std::endl;
std::cout << "thread 2 id: " << t2.get_id() << std::endl;
std::swap(t1, t2);
std::cout << "after std::swap(t1, t2):" << std::endl;
std::cout << "thread 1 id: " << t1.get_id() << std::endl;
std::cout << "thread 2 id: " << t2.get_id() << std::endl;
t1.swap(t2);
std::cout << "after t1.swap(t2):" << std::endl;
std::cout << "thread 1 id: " << t1.get_id() << std::endl;
std::cout << "thread 2 id: " << t2.get_id() << std::endl;
t1.join();
t2.join();
}
可能的输出:
thread 1 id: 1892
thread 2 id: 2584
after std::swap(t1, t2):
thread 1 id: 2584
thread 2 id: 1892
after t1.swap(t2):
thread 1 id: 1892
thread 2 id: 2584