Why did my counter get slower with more threads?

medium~20 min#c++#cache#concurrency#performance

Each of eight threads increments its own counter in a tight loop. The counters live in one array, std::atomic<long> counts[8], one per thread. There is no shared state and no lock.

Throughput is worse than with a single thread. Why, and what do you change?

Solution — locked

Sign in to unlock this one

The solution opens once you explain the idea in your own words and it passes the grader — which needs an account to record. Signing in is free.

Log in