Home All Groups Group Topic Archive Search About

Spot the Bug: Fun Concurrency Bug

Author
24 Oct 2007 6:19 PM
Chris Mullins [MVP - C#]
I hit this bug last night in some test code I had written. It took a few
minutes to figure out what the root cause was, and it left me thinking,
"Wow. That was an interesting one that doesn't come up very often!".

So, for fun, who sees the bug and can explain why it's happening?

List<Thread> threads = new List<Thread>();
for (int i = 0; i < 2; i++)
{
    Thread t = new Thread(delegate()
      {
          Debug.Assert(i != 2);
      });

    t.Start();
    threads.Add(t);
}

foreach (Thread thread in threads)
    thread.Join();

--
Chris Mullins

AddThis Social Bookmark Button