c++ - Issue with std::atomic and custom structs -
can't seem (see code) reason. looked @ documentation, there doesn't seem reason not work...
struct vector { float x, y, z; }; std::atomic<vector> name = {0};
it says can't initialize initializer list, , when go use in code, says has no members.
name.x = 4.f; name.y = 2.f * name.x; name.z = 0.1f;
an instance of std::atomic<vector>
isn't instance of vector
. doesn't have x
, y
, or z
members. have (conceptually, internally) instance of vector
. can't access .
operator because break atomicity, is, like, point of std::atomic
. (this why can't use initializer list.)
to access vector
stuff, use load()
, store()
:
//atomically load snapshot of name auto name_snapshot = name.load(); //name_snapshot vector instance name_snapshot.x = 4.f; name_snapshot.y = 2.f * name_snapshot.x; name_snapshot.z = 0.1f; //now atomically store it: name.store(name_snapshot);
Comments
Post a Comment