C++11 <thread> 详解

在C++11中 <thread>有一个thread类和this_thread的命名空间,std::thread 在 <thread>头文件中声明,因此使用 std:🧵:thread时需要包含 <thread>头文件。

成员有两种类型,一种是id,即该线程的id,另一种是native_handle_type,native的句柄类型。

std:🧵:thread成员函数
(constructor)thread 构造空的thread
(destructor)~thread 结束线程。
operator= Move-assign线程。
get_id 获取线程id。
joinable 检查线程是否可被join。
join join线程。
detach detach线程。
swap swap线程。
native_handle 返回native handle。
hardware_concurrency [static] 检测硬件并发特性。

以及一个非成员过载函数 swap (thread),
void swap (thread& x, thread& y) noexcept;
交换 x 和 y thread对象的状态。

std::thread构造
default (1) 默认构造函数,创建一个空的thread执行对象。
initialization (2) 初始化构造函数,创建一个thread对象,该thread对象可被joinable,新产生的线程会调用fn函数,该函数的参数由args给出。
copy [deleted] (3) 拷贝构造函数(被禁用),意味着thread不可被拷贝构造。
move (4) move构造函数,move构造函数,调用成功之后x不代表任何thread执行对象。

注意:可被 joinable 的 thread 对象必须在他们销毁之前被主线程 join 或者将其设置为 detached.

move 赋值操作
move (1) move赋值操作,如果当前对象不可joinable,需要传递一个右值引用(rhs)给move赋值操作;如果当前对象可被joinable,则terminate() 报错。
copy [deleted] (2) 拷贝赋值操作被禁用,thread对象不可被拷贝。
例子1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>       // std::cout
#include <atomic> // std::atomic
#include <thread> // std::thread
#include <vector> // std::vector

std::atomic<int> global_counter (0);

void increase_global (int n) { for (int i=0; i<n; ++i) ++global_counter; }

void increase_reference (std::atomic<int>& variable, int n) { for (int i=0; i<n; ++i) ++variable; }

struct C : std::atomic<int> {
C() : std::atomic<int>(0) {}
void increase_member (int n) { for (int i=0; i<n; ++i) fetch_add(1); }
};

int main ()
{
std::vector<std::thread> threads;

std::cout << "increase global counter with 10 threads...\n";
for (int i=1; i<=10; ++i)
threads.push_back(std::thread(increase_global,1000));

std::cout << "increase counter (foo) with 10 threads using reference...\n";
std::atomic<int> foo(0);
for (int i=1; i<=10; ++i)
threads.push_back(std::thread(increase_reference,std::ref(foo),1000));

std::cout << "increase counter (bar) with 10 threads using member...\n";
C bar;
for (int i=1; i<=10; ++i)
threads.push_back(std::thread(&C::increase_member,std::ref(bar),1000));

std::cout << "synchronizing all threads...\n";
for (auto& th : threads) th.join();

std::cout << "global_counter: " << global_counter << '\n';
std::cout << "foo: " << foo << '\n';
std::cout << "bar: " << bar << '\n';

return 0;
}

例子2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <utility>
#include <thread>
#include <chrono>
#include <functional>
#include <atomic>

void f1(int n)
{
for (int i = 0; i < 5; ++i) {
std::cout << "Thread " << n << " executing\n";
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
}

void f2(int& n)
{
for (int i = 0; i < 5; ++i) {
std::cout << "Thread 2 executing\n";
++n;
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
}

int main()
{
int n = 0;
std::thread t1; // t1 is not a thread
std::thread t2(f1, n + 1); // pass by value
std::thread t3(f2, std::ref(n)); // pass by reference
std::thread t4(std::move(t3)); // t4 is now running f2(). t3 is no longer a thread
t2.join();
t4.join();
std::cout << "Final value of n is " << n << '\n';
return 0
}