C++如何保证一段初始化的代码只运行一次(C++11 std::call_once)

template <class Fn, class... Args>

  void call_once (once_flag& flag, Fn&& fn, Args&&...args);

需要包含头文件:<mutex>

参数:

(1)flag:是std::once_falg对象(定义一个该对象传进去即可),属于控制的标签,相同的falg只执行一次(见下面详解)

(2)fn:需要只执行一次的函数对象

(3)args:传递给fn函数的参数,如果有就传递,没有就不传递。

下面通过程序来解释flag的作用:

 

#include<iostream>
#include<mutex>
#include<thread>

using namespace std;

void init()
{
  cout << "data has inited" << endl;
}

void fun()
{
  once_flag init_flag; //错误的用法
  call_once(init_flag, init);
}

int main()
{
  thread t1(fun);
  thread t2(fun);
  t1.join();
  t2.join();
  system("pause");
  return 0;
}

 


运行结果:

可以看到输出了两次“data has inited”,这是因为init_flag是定义在fun中的局部变量,每个线程的fun函数中的init_flag是不同的,所有才会输出两次、、如果init_flag是全局变量等,是同一个变量,则就只会输出一次。、、上面所说相同的flag的只执行一次。应该理解了。

 

 

 

 

上面的程序修改后正确的示范:

 

#include<iostream>
#include<mutex>
#include<thread>
using namespace std;
once_flag init_flag;

void init()
{
  cout << "data has inited" << endl;
}

void fun()
{
  call_once(init_flag, init);
}

int main()
{
  thread t1(fun);
  thread t2(fun);
  t1.join();
  t2.join();
  system("pause");
  return 0;
}

 


运行结果:

 

这次就只输出一次了、、说明只执行了一次!

用途:

  std::call_once主要用于多线程的只初始化一次资源的情景,典型的例子就是完美的解决线程安全的单列模式。
————————————————
版权声明:本文为CSDN博主「帝江VII」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_31175231/article/details/77916028

posted @ 2021-08-25 20:27  默行于世  阅读(1428)  评论(0编辑  收藏  举报