【C++ 并发与多线程】1 std::thread id join

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

表示线程的 id
(公开成员类)

成员函数

(构造函数)

构造新的 thread 对象
(公开成员函数)

(析构函数)

析构 thread 对象,必须合并或分离底层线程
(公开成员函数)

operator=

移动 thread 对象
(公开成员函数)

观察器

joinable

检查线程是否可合并,即潜在地运行于平行环境中
(公开成员函数)

get_id

返回线程的 id
(公开成员函数)

native_handle

返回底层实现定义的线程句柄
(公开成员函数)

hardware_concurrency

[静态]

返回实现支持的并发线程数
(公开静态成员函数)

操作

join

等待线程完成其执行
(公开成员函数)

detach

容许线程从线程句柄独立开来执行
(公开成员函数)

swap

交换二个 thread 对象
(公开成员函数)

非成员函数

std::swap(std::thread)

(C++11)

特化 std::swap 算法
(函数模板)

std::thread::id

std::thread::id

定义于头文件 <thread>

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

错误条件

示例

#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


版权声明:本文为HelloZEX原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。