#include <atomic>
#include <cassert>
#include <thread>

int main() {
    std::atomic<int> x = 0;
    std::atomic<int> y = 0;
    bool x_set_before_y = false;
    bool y_set_before_x = false;

    std::thread write_x(
        [&] {
            x = 1;
        }
    );
    std::thread write_y(
        [&] {
            y = 1;
        }
    );
    std::thread read_x_then_y(
        [&] {
            while (x == 0)
                ;
            if (y == 0)
                x_set_before_y = true;
        }
    );
    std::thread read_y_then_x(
        [&] {
            while (y == 0)
                ;
            if (x == 0)
                y_set_before_x = true;
        }
    );

    write_x.join();
    write_y.join();
    read_x_then_y.join();
    read_y_then_x.join();

    assert(!(x_set_before_y && y_set_before_x));
}
