多线程译文01

包含头文件#include <thread>

 

介绍:

thread类代表每个线程的执行。线程的执行时一系列能够同时执行的指令在相同的共享空间中同时执行。

初始化一个thread对象,代表着一个线程开始执行。这是它可以joinable,并且有一个唯一的线程ID。

一个没有被初始化(使用默认构造函数时)的thread对象,不能够joinable,它的线程ID和其他所有不能够joinable的线程一样。

当一个线程对象调用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;
}

 

posted @ 2016-11-28 00:31  郭志凯  阅读(2506)  评论(0编辑  收藏  举报