pc网站转换手机网站代码,常平网站,沭阳网站建设方案,企业建设网站的一般过程C++(11):多线程调用,确保单次执行的call_once_c++ 单次执行函数-CSDN博客 介绍了call_once在多线程竞争的环境下,能确保函数只被执行一次。
call_once在反复被调用时,也可以确保只被调用一次:
#include iostream
#include thread
#include chronoiostream
#include thread
#include chrono
#include mutex
using namespace std;unsigned long getTime()
{return chrono::system_clock::now().time_since_epoch().count()/chrono::system_clock::period::den;
}
void doTask(int id)
{coutgetTime()" doTask:"id" begin"endl;this_thread::sleep_for(chrono::seconds(1));coutgetTime(