#include <thread>
#include <mutex>
#include <array>
#include <print>

int main()
{
    const unsigned long numtimes = 1000*1000*1000UL;
    unsigned long counter = 0;
    std::mutex lock;                                   // <-- used to protect unsafe operator++

    auto inc = [&counter, &lock]() {
        unsigned long n = numtimes;
        while (n--) {
            lock.lock();                               // <-- "acquire"
            counter++;
            lock.unlock();                             // <-- "release"
        }
    };

    std::array threads{
        std::thread(inc),
        std::thread(inc),
    };
    for (auto& t: threads)
        t.join();

    std::println("expected {}, real {}", threads.size()*numtimes, counter);

    return 0;
}
