#include #include #include #include // Simple spinlock built from std::atomic_flag. struct spinlock { std::atomic_flag flag = ATOMIC_FLAG_INIT; void lock() { while (flag.test_and_set(std::memory_order_acquire)) { // busy‑wait } } void unlock() { flag.clear(std::memory_order_release); } }; constexpr int increments = 100'000; spinlock mtx; int protected_value = 0; void worker() { for (int i = 0; i < increments; ++i) { mtx.lock(); ++protected_value; mtx.unlock(); } } int main() { std::thread t1(worker); std::thread t2(worker); t1.join(); t2.join(); std::cout << "protected value = " << protected_value << "\n"; // EXPECT: protected value = 200000 return 0; }