在多线程的环境下,有些时候我们不需要某给函数被调用多次或者某些变量被初始化多次,它们仅仅只需要被调用一次或者初始化一次即可。很多时候我们为了初始化某些数据会写出如下代码,这些代码在单线程中是没有任何问题的,但是在多线程中就会出现不可预知的问题。
bool initialized = false; // global flag
if (!initialized) {
// initialize if not initialized yet
initialize ();
initialized = true;
}
or
static std::vector<std::string> staticData;
void foo ()
{
if (staticData.empty ()) {
staticData = initializeStaticData ();
}
...
}
为了解决上述多线程中出现的资源竞争导致的数据不一致问题,我们大多数的处理方法就是使用互斥锁来处理。在C++11中提供了最新的处理方法:使用std::call_once函数来处理,其定义如下头文件#include
template< class Function, class... Args >
void call_once ( std::once_flag& flag, Function&& f, Args&& args... );
参数解析:
flag - an object, for which exactly one function gets executed
f - 需要被调用的函数
args... - 传递给函数f的参数(可以多个)
返回值为 (none)
抛出异常
std::system_error if any condition prevents calls to call_once from executing as specified any exception thrown by f
实例1:
#include <iostream>
#include <thread>
#include <mutex>
std::once_flag flag1;
void simple_do_once()
{
std::call_once(flag1, [](){ std::cout << "Simple example: called once\n"; });
}
int main()
{
std::thread st1(simple_do_once);
std::thread st2(simple_do_once);
std::thread st3(simple_do_once);
std::thread st4(simple_do_once);
st1.join();
st2.join();
st3.join();
st4.join();
}
实例2——单例模式:
className.h文件
class className
{
public:
className(); //构造函数
~className() = default;
className(const className &) = delete;
className &operator=(const className &) = delete;
static std::once_flag _flag;
static className* GetInstance(void);
public:
...
private:
static className *变量名;
};
className.cpp
className*className:: *变量名= NULL;
className::className()
{
}
className* className::GetInstance(void)
{
std::call_once(_flag, [&]() {
变量名= new className();
});
return 变量名;
}
更详细介绍可参考:
C++11中once_flag,call_once实现分析
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/157451.html