#include #include #include #include #include class Counter { public: Counter() : counter(0) { } unsigned long long int increment() { std::lock_guard lock(mutex); return ++counter; } private: std::mutex mutex; unsigned long long int counter; }; int main() { constexpr unsigned int nof_threads = 2; using CounterValue = unsigned long long int; constexpr CounterValue max_counter = 1LL << 24; std::vector threads(nof_threads); hpc::aux::WallTime wall_time; Counter counter; wall_time.tic(); for (auto& t: threads) { t = std::thread([&]{ while (counter.increment() < max_counter) ; }); } for (auto& t: threads) { t.join(); } auto t = wall_time.toc(); std::printf("avg time per lock = %.2lf ns\n", t / max_counter * 1000000000LL); }