atomic是在多线程编程中经常使用的一个类。使用的时候需要包含<atomic>文件。
atomic
atomic内部封装一个值,并保证对该值的访问的互斥性。可以用来进行线程同步。使用非常方便。说白了就是atomic内部保存的值是线程安全的,不用担心多线程访问时的同步问题。也就是说保证对内部值的读取和保存的操作的原子性。
atomic是个模板类,可以保存的常用的值的类型(这些类型在atomic头文件中都有自己的定义)如下表:
| 类型 | atomic内部定义 | 描述 |
| bool | atomic_bool | |
| char | atomic_char | atomics for fundamental integral types. These are either typedefs of the corresponding full specialization of the atomic class template or a base class of such specialization. |
| signed char | atomic_schar | |
| unsigned char | atomic_uchar | |
| short | atomic_short | |
| unsigned short | atomic_ushort | |
| int | atomic_int |
C++11中的原子操作(atomic)详解

C++11的<atomic>库提供了一种在多线程环境中保证数据访问互斥性的机制。atomic类确保了其内部值的读取和更新操作的原子性,消除线程同步问题。本文将探讨atomic的构造函数、赋值操作符、store函数和load函数的用法。
最低0.47元/天 解锁文章
2万+

被折叠的 条评论
为什么被折叠?



