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 | |
unsigned int | atomic_uint | |
long | atomic_lon |