dimanche 5 mars 2023

Get result for relaxed memory model c++ code?

void test_relaxed()
{
    atomic<int> x{0};
    atomic<int> y{0};

    std::thread t1([&] {
        auto r1 = y.load(memory_order_relaxed); //a
        x.store(r1, memory_order_relaxed); //b
        });

    std::thread t2([&] {
        auto r2 = x.load(memory_order_relaxed); //c
        y.store(42, memory_order_relaxed); //d
    });
    
    t1.join();
    t2.join();
}

According to cppreference, the above code is allowed to produce r1 == r2 == 42. But I haved test it on x86-64 and arm64 platform, I can not get this result. Is there any way to get it?

Aucun commentaire:

Enregistrer un commentaire