#include <iostream>
#include <chrono>
#include <thread>
#include <atomic>
using namespace std;
using namespace std::chrono;

struct spin_lock {
    std::atomic_flag flag;

    void lock() {
        while (flag.test_and_set(std::memory_order_acquire))
            ;
    }

    void unlock() {
        flag.clear(std::memory_order_release);
    }
};

int main() {
    uint64_t counter = 0;
    spin_lock mtx;

    auto f = [&]() {
        for (uint64_t i = 0; i < 1'000'000'000; ++i) {
            mtx.lock();
            ++counter;
            mtx.unlock();
        }
    };

    {
        jthread t0 { f };
        jthread t1 { f };
    }

    cout << "result: " << counter << '\n';
}
