1 静态变量结合函数调用方式
通常我们使用 std::call_once
配合 std::once_flag
使用,这里有更简单的方法——静态变量结合函数调用,如下所示:
static auto onece_call = [&]()->int {
//onece call code
//这里添加需要仅执行一次的代码
return 0;
}();
c++11后保证多线程中局部静态变量也只会初始化一次。上面代码中最后的小括号表示执行前面定义的lambda函数。
2 std::call_once
配合 std::once_flag
使用的方式
std::once_flag
定义于头文件 | ||
class once_flag; |
类 std::once_flag
是 std::call_once 的辅助类。
传递给多个 std::call_once 调用的 std::once_flag
对象允许那些调用彼此协调,从而只令调用之一实际运行完成。这个很重要,可以使得多个调用分支只有一个执行。
std::once_flag
既不可复制亦不可移动。
成员函数
std::once_flag::once_flag
constexpr once_flag() noexcept; | ||
构造 once_flag
对象。设置内部状态为指示尚未调用函数。
参数
(无)
std::call_once
定义于头文件 | ||
template< class Callable, class... Args > |
准确执行一次可调用 (Callable) 对象 f
,即使同时从多个线程调用。
细节为:
- 若在调用
call_once
的时刻,flag
指示已经调用了f
,则call_once
立即返回(称这种对call_once
的调用为消极)。
- 否则,
call_once
以参数 std::forward<Args>(args)... 调用 std::forward<Callable>(f) (如同用 std::invoke )。不同于 std::thread 构造函数或 std::async ,不移动或复制参数,因为不需要转移它们到另一执行线程(称这种对call_once
的调用为积极)。
- 若该调用抛异常,则传播异常给
call_once
的调用方,并且不翻转flag
,以令其他调用将得到尝试(称这种对call_once
的调用为异常)。 - 若该调用正常返回(称这种对
call_once
的调用为返回),则翻转flag
,并保证以同一flag
对call_once
的其他调用为消极。
同一 flag
上的所有积极调用组成单独全序,它们由零或多个异常调用后随一个返回调用组成。该顺序中,每个积极调用的结尾同步于下个积极调用。
从返回调用的返回同步于同一 flag
上的所有消极调用:这表示保证所有对 call_once
的同时调用都观察到积极调用所做的任何副效应,而无需额外同步。
参数
flag | - | 对象,对于它只有一个函数得到执行 |
f | - | 要调用的可调用 (Callable) 对象 |
args... | - | 传递给函数的参数 |
返回值
(无)
异常
- 若任何条件阻止对
call_once
的调用按规定执行,则抛出 std::system_error - 任何
f
所抛的异常
注解
若对 call_once
的同时调用传递不同的 f
,则调用哪个 f
是未指定的。被选择函数运行于与传递它的 call_once
的调用相同的线程。
即使在从多个线程调用时,也保证函数局域静态对象的初始化仅出现一次,这可能比使用 std::call_once
的等价代码更为高效。
此函数的 POSIX 类似物是 pthread_once 。
示例
#include <iostream>
#include <thread>
#include <mutex>
std::once_flag flag1, flag2;
void simple_do_once()
{
std::call_once(flag1, [](){ std::cout << "Simple example: called once\n"; });
}
void may_throw_function(bool do_throw)
{
if (do_throw) {
std::cout << "throw: call_once will retry\n"; // 这会出现多于一次
throw std::exception();
}
std::cout << "Didn't throw, call_once will not attempt again\n"; // 保证一次
}
void do_once(bool do_throw)
{
try {
std::call_once(flag2, may_throw_function, do_throw);
}
catch (...) {
}
}
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();
std::thread t1(do_once, true);
std::thread t2(do_once, true);
std::thread t3(do_once, false);
std::thread t4(do_once, true);
t1.join();
t2.join();
t3.join();
t4.join();
}
使用glib库的g_once_init_enter、g_once_init_leave
GType
flatpak_uninstall_flags_get_type (void)
{
static gsize static_g_flags_type_id = 0;
if (g_once_init_enter (&static_g_flags_type_id))
{
static const GFlagsValue values[] = {
{ FLATPAK_UNINSTALL_FLAGS_NONE, "FLATPAK_UNINSTALL_FLAGS_NONE", "none" },
{ FLATPAK_UNINSTALL_FLAGS_NO_PRUNE, "FLATPAK_UNINSTALL_FLAGS_NO_PRUNE", "no-prune" },
{ FLATPAK_UNINSTALL_FLAGS_NO_TRIGGERS, "FLATPAK_UNINSTALL_FLAGS_NO_TRIGGERS", "no-triggers" },
{ 0, NULL, NULL }
};
GType g_define_type_id =
g_flags_register_static (g_intern_static_string ("FlatpakUninstallFlags"), values);
g_once_init_leave (&static_g_flags_type_id, g_define_type_id);
}
return static_g_flags_type_id;
}