std::thread 概述

std::thread

thread类表示各个线程的执行。
 
在多线程环境下,一个线程和其他线程同时执行指令序列,并共享地址空间。
 
一个被初始化的线程对象代表一个正在执行的线程。比如一个线程对象是可连接的,它有一个唯一的线程ID。

一个默认的没有初始化的线程对象不是可链接的,它的线程ID时和其他没有可连接的线程公用的。
 
一个可链接的线程变为不可链接的线程或者当对它调用join或者detach。

Member types


Member functions


Non-member overloads


Example

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
// thread example
#include <iostream>       // std::cout
#include <thread>         // std::thread
 
void foo() 
{
  // do stuff...
}

void bar(int x)
{
  // do stuff...
}

int main() 
{
  std::thread first (foo);     // spawn new thread that calls foo()
  std::thread second (bar,0);  // spawn new thread that calls bar(0)

  std::cout << "main, foo and bar now execute concurrently...
";

  // synchronize threads:
  first.join();                // pauses until first finishes
  second.join();               // pauses until second finishes

  std::cout << "foo and bar completed.
";

  return 0;
}


Output:
main, foo and bar now execute concurrently...
foo and bar completed.
 

thread

 
原文地址:https://www.cnblogs.com/chengyuanchun/p/5266114.html