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

struct wallet {
    std::atomic<uint64_t> money{0};
    operator uint64_t() const {
        return money;
        // return money.load(std::memory_order_relaxed);
    }

    wallet& operator++() {
        ++money;
        // money.fetch_add(1, std::memory_order_relaxed);
        return *this;
    }
};

// Built-in types are always lock-free, but custom structs may not be.
static_assert(atomic<uint64_t>::is_always_lock_free);

void f(wallet& sum, uint64_t count) {
    while (count--) {
        ++sum;
    }
}

int main() {
    constexpr uint64_t count = 1'000'000;
    wallet sum;

    auto time0 = high_resolution_clock::now();

    {
        jthread t1 { f, ref(sum), count };
        jthread t2 { f, ref(sum), count };
    }

    auto time1 = high_resolution_clock::now();
    auto dt = duration_cast<microseconds>(time1 - time0);
    cout << "elapsed: " << dt.count() << " microsec\n";
    cout << "result: " << sum << '\n';
}
