标准的原子波和原子旗的区别。

时间:2021-09-05 00:36:39

I wasn't aware of the std::atomic variables but was aware about the std::mutex (weird right!) provided by the standard; however one thing caught my eye: there are two seemingly-same(to me) atomic types provided by the standard, listed below:

我不知道std::原子变量,但知道标准提供的std:::mutex(奇怪的右!)然而,有一件事引起了我的注意:在我看来,标准提供的原子类型有两种:

  1. std::atomic<bool> type

    std::原子< bool >类型

  2. std::atomic_flag type

    std::atomic_flag类型

Also it is mentioned with the example of std::atomic_flag type -

还有一个std::atomic_flag类型-的例子

std::atomic_flag is an atomic boolean type. Unlike all specializations of std::atomic, it is guaranteed to be lock-free. Unlike std::atomic, std::atomic_flag does not provide load or store operations.

atomic_flag是一种布尔类型。与std::atomic的所有专门化不同,它保证是无锁的。不同于std::原子,std::atomic_flag不提供负载或存储操作。

which I fail to understand. Is std::atomic bool type not guaranteed to be lock-free? Then it's not atomic or what?

我不明白。std::原子bool类型不保证是无锁的吗?那么它不是原子还是什么?

So what's the difference between the two and when should I use which?

那么这两者之间的区别是什么?

2 个解决方案

#1


22  

std::atomic bool type not guranteed to be lock-free?

原子bool类型不保证无锁?

Correct. std::atomic may be implemented using locks.

正确的。可以使用锁实现原子化。

then it's not atomic or what?

那么它不是原子还是什么?

std::atomic is atomic whether it has been implemented using locks, or without. std::atomic_flag is guaranteed to be implemented without using locks.

原子是原子的,无论是否使用锁实现。std::atomic_flag可以在不使用锁的情况下实现。

So what's the difference b/w two

b/w 2的差是多少

The primary difference besides the lock-free guarantee is:

除了无锁保证外,主要的区别是:

std::atomic_flag does not provide load or store operations.

atomic_flag不提供加载或存储操作。


and when should I use which?

什么时候用哪个?

Usually, you will want to use std::atomic<bool> when you need an atomic boolean variable. std::atomic_flag is a low level structure that can be used to implement custom atomic structures.

通常,当您需要一个原子布尔变量时,您需要使用std::atomic 。atomic_flag是一种低层结构,可以用来实现自定义原子结构。

#2


18  

std::atomic<T> guarantees that accesses to the variable will be atomic. It however does not says how is the atomicity achieved. It can be using lock-free variable, or using a lock. The actual implementation depends on your target architecture and the type T.

原子 保证对变量的访问是原子的。但它并没有说明原子性是如何实现的。它可以使用无锁变量,也可以使用锁。实际实现取决于目标体系结构和类型T。

std::atomic_flag on the other hand is guaranteed to be implemented using a lock-free technique.

另一方面,atomic_flag保证使用无锁技术来实现。

#1


22  

std::atomic bool type not guranteed to be lock-free?

原子bool类型不保证无锁?

Correct. std::atomic may be implemented using locks.

正确的。可以使用锁实现原子化。

then it's not atomic or what?

那么它不是原子还是什么?

std::atomic is atomic whether it has been implemented using locks, or without. std::atomic_flag is guaranteed to be implemented without using locks.

原子是原子的,无论是否使用锁实现。std::atomic_flag可以在不使用锁的情况下实现。

So what's the difference b/w two

b/w 2的差是多少

The primary difference besides the lock-free guarantee is:

除了无锁保证外,主要的区别是:

std::atomic_flag does not provide load or store operations.

atomic_flag不提供加载或存储操作。


and when should I use which?

什么时候用哪个?

Usually, you will want to use std::atomic<bool> when you need an atomic boolean variable. std::atomic_flag is a low level structure that can be used to implement custom atomic structures.

通常,当您需要一个原子布尔变量时,您需要使用std::atomic 。atomic_flag是一种低层结构,可以用来实现自定义原子结构。

#2


18  

std::atomic<T> guarantees that accesses to the variable will be atomic. It however does not says how is the atomicity achieved. It can be using lock-free variable, or using a lock. The actual implementation depends on your target architecture and the type T.

原子 保证对变量的访问是原子的。但它并没有说明原子性是如何实现的。它可以使用无锁变量,也可以使用锁。实际实现取决于目标体系结构和类型T。

std::atomic_flag on the other hand is guaranteed to be implemented using a lock-free technique.

另一方面,atomic_flag保证使用无锁技术来实现。