cppreference说这std::memory_order_acquire
将防止读写在它之前重新排序。所以我可以std::memory_order_acquire
只用于防止在运行时重新排序而没有相应的std::memory_order_release
. 例如
#include <atomic>
#include <stdint.h>
std::atomic<uint32_t> g_counter{};//Counter for thread in Fun
std::atomic<void*> g_data{};
void Fun()
{
g_counter.fetch_add(1, std::memory_order_acquire);//Warning:`fetch_add` maybe reorder after next 'load'
void *data = g_data.load(std::memory_order_acquire);
g_counter.fetch_sub(1, std::memory_order_relaxed);//just memory_order_relaxed will be ok
}
真的吗?