如何在C ++中创建原子枚举?

如何在C ++中创建原子枚举?

问题描述:

atomic 包含许多不同变量类型的原子版本。但是,它不包含原子枚举类型。有没有办法使用原子枚举或使我自己?就我可以告诉,我唯一的选择是不使用枚举或使用互斥体/信号量保护他们。

Class atomic contains atomic versions of many different variable types. However, it doesn't contain an atomic enum type. Is there a way to use atomic enums or make my own? As far as I can tell, my only option is to either not use enums or use mutexes/semaphores to protect them.

注意:这 bug report 我发现提到std :: atomic枚举支持,但我没有看到任何提及的原子枚举类型在

Note: This bug report I found mentions "std::atomic enum support", but I don't see any mention of an atomic enum type in the C++ Standard, so I'm not sure what that refers to.

您可以创建一个原子枚举,如下所示:

You can create an atomic enum like this:

#include <atomic>

enum Decision {stay,flee,dance};
std::atomic<Decision> emma_choice {stay}; // emma_choice is atomic

你也可以对枚举类做同样的事情:

You can also do the same thing with enum classes:

#include <atomic>

enum class Decision {stay,flee,dance};
std::atomic<Decision> emma_choice {Decision::stay}; // emma_choice is atomic