c++11 thread

官方例子

// thread example
#include <iostream>       // std::cout
#include <thread>         // std::thread
#include <unistd.h>

using namespace std;

void foo() 
{
        // do stuff...
        for(int i = 0 ; i < 10 ; ++i){
                cout << i << endl;
                sleep(1);
        }
}

void bar(int x)
{
        // do stuff...
        for(int i = 0 ; i < 10 ; ++i){
                cout << i << endl;
                sleep(1);
        }
}

int main() 
{
        std::thread *first = new thread(bar,2);     // 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

        delete first;
        std::cout << "foo and bar completed.
";
        return 0;
}
原文地址:https://www.cnblogs.com/nanqiang/p/13065881.html