#include <iostream>
#include <atomic>
#include <thread>
std::atomic<int> counter(0);
void increment_if_zero() {
int expected = 0;
if (counter.compare_exchange_strong(expected, 100)) {
std::cout << "Counter was zero, updated to 100" << std::endl;
} else {
std::cout << "Counter was not zero, value is " << counter.load() << std::endl;
}
}
int main() {
std::thread t1(increment_if_zero);
std::thread t2(increment_if_zero);
t1.join();
t2.join();
std::cout << "Final counter value: " << counter << std::endl;
return 0;
}