C++11多线程编程 call_once

来源:互联网 发布:淘宝天猫超市优惠券 编辑:程序博客网 时间:2024/06/14 21:56

call_once 可以使他所修饰的函数在多线程环境中只执行一次,call_once((once_falg)x,y),他接受一个once_falg变量参数,另一个参数可以使函数,lambda表达式等。

上代码:

#include <iostream>#include<thread>#include<mutex>using namespace std;once_flag t;void fun1(){   cout<<"i an ready"<<endl;}void do_once(){ call_once(t,fun1); cout<<" hello"<<endl;}int main(){    thread t1(do_once);    thread t2(do_once);    thread t3(do_once);    t1.join();    t2.join();    t3.join();    return 0;}


0 0