I have a struct which contains two data members:
struct A{
short b;
short c;
};
and a vector containing many of these struct objects:
std::vector<A> vec;
at runtime the data members A.b
and A.c
from struct objects are set to zero/a value x
. However, b
and c
must be modified at the same time- one cannot be updated separately without the other. I was planning on using an atomic compare_exchange_weak()
to do the update.
I am not sure whether I should represent each struct as an std::atomic<A>
in the vector, or whether I should place a union inside the struct, to combine the two shorts in to a single uint32_t
and then modify this:
union A {
struct {
short b;
short c;
};
uint32_t d;
};
What would be the best solution? Should I store a vector of:
std::vector<uint32_t>
and upon accessing each element, reinterpret_cast to A, to obtain d
?
I would like the locking to be as least-intrusive as possible.
Portability is not required, this will be on Linux, 64-bit, x86, GCC 4.8+ compiler