如何使用 std::atomic 将联合设置为原子的?还是我必须将工会的成员声明为原子的?
typedef union {
int integer;
float flt;
double dbl;
int *intArray;
float *floatArray;
unsigned char *byteArray;
} ValueUnion;
class FooClass {
public:
std::atomic<ValueUnion> value;
};
访问联合会报错:
foo->value.floatArray = NULL;
error: no member named 'floatArray' in 'std::__1::atomic<ValueUnion>'
foo->value.floatArray = NULL;
我是否需要做类似的事情:
typedef union {
std::atomic<int> integer;
std::atomic<float> flt;
std::atomic<double> dbl;
std::atomic<int*> *intArray;
std::atomic<float*> *floatArray;
std::atomic<unsigned char*> *byteArray;
} ValueUnion;
并声明成员变量值如下?
class FooClass {
public:
ValueUnion value;
};