关注此问题-std::memory_order_relaxed and initialization。假设我有这样的代码
class Something {
public:
int value;
};
auto&& pointer = std::atomic<Something*>{nullptr};
// thread 1
auto value = Something{1};
pointer.set(&value, std::memory_order_relaxed);
// thread 2
Something* something = nullptr;
while (!(something = pointer.load(std::memory_order_relaxed))) {}
cout << something->value << endl;
这可以保证打印1吗?可以允许实现采用未初始化值的地址吗?
(假设线程2读取线程1设置的指针没有生存期问题)