c++新特性之std::atomic
2022/5/30 5:22:00
本文主要是介绍c++新特性之std::atomic,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
std::atomic为C++11封装的原子数据类型, 支持多种类型数据的原子操作,以下是atomic支持的数据类型,摘自http://www.cplusplus.com/reference/atomic/。
#include <atomic> #include <thread> #include <list> std::atomic_int g_iCount = 100; void threadfunc1() { while (g_iCount > 0) { printf("threadfunc1 g_iCount:%d\r\n", --g_iCount); } } void threadfunc2() { while (g_iCount > 0) { printf("threadfunc2 g_iCount:%d\r\n", --g_iCount); } } int main(int argc, char** argv) { thread t1(threadfunc1); thread t2(threadfunc1); thread t3(threadfunc2); thread t4(threadfunc2); t1.join(); t2.join(); t3.join(); t4.join(); return 0; }
以上初始化4线程,用来运行int类型的g_iCount的递减操作,未出现任何数据错误的情况。
这篇关于c++新特性之std::atomic的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-10-07fastcgi 是什么-icode9专业技术文章分享
- 2024-10-07fastcgi 的详细使用教程介绍-icode9专业技术文章分享
- 2024-10-07git如何更新单个文件到本地-icode9专业技术文章分享
- 2024-10-07如何使用ASM(Abstract Syntax Tree Manipulation)技术来修改第三方AAR依赖中的函数-icode9专业技术文章分享
- 2024-10-07Activity 跳转时间耗时很长怎么优化解决-icode9专业技术文章分享
- 2024-10-07Androud Toast 有哪些常用的第三方组件-icode9专业技术文章分享
- 2024-10-07在viewmodel中怎么使用 mmkv?-icode9专业技术文章分享
- 2024-10-07MMKV.defaultMMKV() 是单例模式吗?-icode9专业技术文章分享
- 2024-10-04el-table 开启定时器下,表格的选中状态会消失是什么原因-icode9专业技术文章分享
- 2024-10-03如何安装和初始化飞牛私有云 fnOS?-icode9专业技术文章分享