How can I create an atomic enum in C++?
atomic, c++, c++11, enums
Solution
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
Problem
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. 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.