首页 > 编程语言 > 详细

c++11 thread

时间:2020-06-08 15:49:19      阅读:27      评论:0      收藏:0      [点我收藏+]

官方例子

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

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

        delete first;
        std::cout << "foo and bar completed.\n";
        return 0;
}

 

c++11 thread

原文:https://www.cnblogs.com/nanqiang/p/13065881.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!