std:call_once是C++11引入的新特性,如需使用,只需要#include <mutex>即可,简单来说std:call_once的作用,确保函数或代码片段在多线程环境下,只需要执行一次,常用的场景如Init()操作或一些系统参数的获取等。
相对来说,std::call_once用法比较简单,配合std::once_flag即可实现,废话不多说,上代码:
#include "stdafx.h"
#include <iostream>
#include <thread>
#include <mutex>
std::once_flag flag;
void Initialize()
{
std::cout << "Run into Initialize.." << std::endl;
}
void Init()
{
std::call_once(flag, Initialize);
}
int main()
{
std::thread t1(Init);
std::thread t2(Init);
std::thread t3(Init);
std::thread t4(Init);
t1.join();
t2.join();
t3.join();
t4.join();
}
执行结果如下:
可以看到,Initialize()只调用了一次。
需要了解更多的话,可以参考https://en.cppreference.com/w/cpp/thread/call_once关于std::call_once的描述:
Executes the Callable object f
exactly once, even if called concurrently, from several threads.
In detail:
call_once
is called, flag
indicates that f
was already called, call_once
returns right away (such a call to call_once
is known as passive).call_once
is known as active).call_once
, and the flag is not flipped so that another call will be attempted (such a call to call_once
is known as exceptional).call_once
is known as returning), the flag is flipped, and all other calls to call_once
with the same flag are guaranteed to be passive.All active calls on the same flag
form a single total order consisting of zero or more exceptional calls, followed by one returning call. The end of each active call synchronizes-with the next active call in that order.
The return from the returning call synchronizes-with the returns from all passive calls on the same flag
: this means that all concurrent calls to call_once
are guaranteed to observe any side-effects made by the active call, with no additional synchronization.
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有