public void ChangeState(int loop)
{
// lock (sync)
{
if (_state == 5)
{
_state++;
Trace.Assert(_state == 6, $"Race condition occurred after {loop} loops");
}
_state = 5;
}
}
}
public void Deadlock1() { int i = 0; while (true) { WriteLine("1 - waiting for s1"); lock (_s1) { WriteLine("1 - s1 locked, waiting for s2"); lock (_s2) { WriteLine("1 - s1 and s2 locked, now go on..."); _s1.ChangeState(i); _s2.ChangeState(i++); WriteLine($"1 still running, i: {i}"); } } } }