#include #include #include #include #include #include constexpr unsigned kNumThreads = 8; constexpr unsigned kIter = 1 << 18; static_assert((static_cast(kNumThreads) * static_cast(kIter)) <= std::numeric_limits::max(), "Expectate result overflowed!"); int main() { owning_mutex data(0u); std::vector threads; threads.reserve(kNumThreads); for (unsigned t = 0; t < kNumThreads; ++t) { threads.emplace_back([&data, t]() { for (unsigned i = 0; i < kIter; ++i) { *data.lock() += 1; } std::printf("th%u finished\n", t); }); } for (auto& th : threads) { th.join(); } assert(*data.lock() == (kNumThreads * kIter)); std::printf("Result %u\n", *data.lock()); return 0; }