C++并发编程(3):移交线程归属权

C++并发编程(3)和C++并发编程(2)对应书中第二章,C++并发编程(3)是第二章的收尾部分

移交线程归属权

对于一个具体的执行线程,其归属权可在多个thread实例间转移,有以下几种用法

  1. 在thread实例间转移线程所属权
  2. thread对象做返回值
  3. thread对象作为函数参数
  4. 向STL容器中装载thread对象

示例代码如下:

#include 
#include 
#include 
#include 

using namespace std;

const int thread_num = 10;

void smile()
{
   
    cout << "Smile" << endl;
}

void cry()
{
   
    cout << "cry" << endl;
}

thread test()
{
   
    thread t_(smile);

    cout << "test t_ id:" << t_.get_id() << endl

你可能感兴趣的:(C++,c++,并发编程)