如何在C ++中创建原子枚举? [英] How can I create an atomic enum in C++?

查看:178
本文介绍了如何在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

这篇关于如何在C ++中创建原子枚举?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆