多线程译文01

包含头文件#include <thread>ios

 

介绍:函数

thread类表明每一个线程的执行。线程的执行时一系列可以同时执行的指令在相同的共享空间中同时执行。spa

初始化一个thread对象,表明着一个线程开始执行。这是它能够joinable,而且有一个惟一的线程ID。线程

一个没有被初始化(使用默认构造函数时)的thread对象,不可以joinable,它的线程ID和其余全部不可以joinable的线程同样。
code

当一个线程对象调用join()或者detach()方法后,这个线程对象就变得不可以joinable。对象

 

// 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...\n";

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

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

  return 0;
}

相关文章
相关标签/搜索