find a thread safety bug
✓ Published0🌍 Public
RRestuta
Last edited Feb 8, 2012
Created on Feb 8, 2012
This example demonstrates a subtle thread-safety bug in a C# .NET console application. Two `ThreadUnsafe` instances execute a `Go()` method concurrently across 10,000 iterations, with a shared static `_val2` flag reset after each attempt. The code uses the `lock` keyword on a `syncObj` to guard the division `_val1/_val2`, but the assignment `_val2 = 0` occurs outside the lock. The visualization—rendered via console output—shows the iteration index and an `AggregateException` when a race condition triggers an arithmetic error, revealing the unprotected write that breaks the intended synchronization.
AI-generated description