win系统下 CPP多线程开发中thread是常用的多线程技术。微软官方介绍地址thread。
首先 看关键声明,#include<thread>中
| 方法名 | 功能 | 说明 |
|---|---|---|
| thread() | 默认构造函数 | 构造一个空的,那么如果需要控制哪个功能进行多线程,就需要用下面的 方法进行初始化 |
| thread(_Fn&& _Fx, _Args&&... _Ax) | 带不定参数的构造函数 | F 线程执行的应用程序中定义的函数。 A 传递给参数列表 F。 多线程的参数可以根据需求不定数量的传参设定 |
| thread(thread&& _Other) | 用其他线程构造参数 | |
| thread& operator=(thread&& _Other) | 重载等号初始化对象 | |
| thread(const thread&) = delete; | ||
| thread::detach |
分离 thread 对象关联的线程 |
分离关联线程。 操作系统将负责释放终止线程的资源。 分离后的线程 无法再使用thread控制 |
| thread::get_id |
返回关联线程的唯一标识符 | 有个线程ID便可以控制这个线程 |
| thread::hardware_concurrency |
静态的。 返回硬件线程上下文的数目的估计值。 |
|
| thread::join |
阻塞,直到关联的线程完成。 | 如果任务分块执行 需要所有任务块执行完后再进行下面的操作,则需要它 |
| thread::joinable | 指定关联的线程是否可连接。 | |
| thread::native_handle |
返回表示线程句柄的特殊类型的实现。 |
|
| thread::swap 方法 |
交换与指定的 thread 对象的状态。 |
|
演示例程:
std::thread th1([]() {surf->detectAndCompute(a, Mat(), key1, c);});std::thread th2([]() {surf->detectAndCompute(b, Mat(), key2, d);});th1.join();th2.join();
如果代码太长 上面的操作就有点不合适,也可以这么写 有无传参两种方式
void fun()
{
}
void fun1(int Val)
{
}
std::thread th1(fun);
std::thread th2(fun1,val);
th1.join();
th2.join();