I am writing a copy constructor for a data structure which needs to copy two std::atomic<T>
members into a new object. While the process doesn't necessarily have to be atomic in my use-case, I would prefer to have the most correct solution possible.
I am aware that the copy constructor is explicitly deleted with std::atomic<T>
so as to force users to use the atomic interface.
atomic(const atomic&) = delete;
What I am currently I am doing something like this:
SomeObject(const SomeObject& other):
_atomic1(other._atomic1.load()),
_atomic2(other._atomic2.load()) {
...
}
I do not believe this operation is atomic, nor do I know a way to make is so (without locks).
Is there a way to copy these values atomically (without locks)?