Counter reads low
Review this C++ worker-pool counter.
What a strong answer looks like
Separate real bugs from style. Rank issues by severity, point at the root cause rather than the symptom, and suggest a concrete fix, specific and kind.
0:00 of about 26 min
Mark a line and say what kind of problem it is.0 findings
1#include <thread>
2#include <vector>
3#include <iostream>
4
5long counter = 0;
6
7void work(int iters) {
8 for (int i = 0; i < iters; ++i) counter++;
9}
10
11int main() {
12 std::vector<std::thread> ts;
13 for (int t = 0; t < 8; ++t) ts.emplace_back(work, 1000000);
14 for (auto& th : ts) th.join();
15 std::cout << counter << "\n"; // expect 8000000
16 return 0;
17}
Which questions mattered is sealed until you submit. Telling you now would just be handing over the edge cases.
Run or narrate your approach, then ask the coach.