Hard
Two threads incrementing the same non-atomic int
Two threads both run incrementer(), concurrently incrementing the same plain (non-atomic) int with no synchronization. How does this differ from the false-sharing example above, and what's the most accurate prediction for this program?
main.cpp
int counter = 0; // plain int, not std::atomic
void incrementer() {
for (int i = 0; i < 1000000; ++i) ++counter;
}
// Thread 1: incrementer();
// Thread 2: incrementer();
// main: t1.join(); t2.join(); std::cout << counter;16 of 51 in Concurrency