IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> C++11特性之std:call_once介绍 -> 正文阅读

[C++知识库]C++11特性之std:call_once介绍

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:

  • If, by the time??call_once????is called,??flag????indicates that??f????was already called,??call_once????returns right away (such a call to??call_once???is known aspassive).
  • Otherwise, call_once invokes???std::forward??<Callable>(f)with the arguments???std::forward??<Args>(args)...?(as if by??std::invoke???). Unlike the??std::thread????constructor or??std::async???, the arguments are not moved or copied because they don't need to be transferred to another thread of execution. (such a call to??call_once???is known asactive).
  • If that invocation throws an exception, it is propagated to the caller of??call_once???, and the flag is not flipped so that another call will be attempted (such a call to??call_once???is known asexceptional).
  • If that invocation returns normally (such a call to??call_once???is known asreturning), the flag is flipped, and all other calls to??call_once???with the same flag are guaranteed to bepassive.

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.

?

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2022-05-14 09:47:23  更:2022-05-14 09:48:18 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/10 5:34:10-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码