How can I create an atomic enum in C++?

前端 未结 2 1804
忘了有多久
忘了有多久 2021-01-03 20:50

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

2条回答
  •  一整个雨季
    2021-01-03 21:38

    You can create an atomic enum like this:

    #include 
    
    enum Decision {stay,flee,dance};
    std::atomic emma_choice {stay}; // emma_choice is atomic
    

    You can also do the same thing with enum classes:

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

提交回复
热议问题