包含頭文件#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; }