![]() | |
![]() |
| | Thread Tools | Search this Thread | Display Modes |
#11
| |||
| |||
|
|
Chris Mullins [MVP - C#] <cmull... (AT) yahoo (DOT) com> wrote: 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(); Posting without reading any responses... "i" is captured and only has a single "instance", so the assertion will fail if the thread executes after the loop has "finished". The solution: List<Thread> threads = new List<Thread>(); for (int i = 0; i < 2; i++) { int j=i; // THIS IS THE CHANGE Thread t = new Thread(delegate() { Debug.Assert(j != 2); // AND THIS }); t.Start(); threads.Add(t); } foreach (Thread thread in threads) thread.Join(); There's a new instance of "j" each time we go round the loop. -- Jon Skeet - <sk... (AT) pobox (DOT) com>http://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet If replying to the group, please do not mail me too |
#12
| |||
| |||
|
|
Chris Mullins [MVP - C#] <cmullins (AT) yahoo (DOT) com> wrote: 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(); Posting without reading any responses... "i" is captured and only has a single "instance", so the assertion will fail if the thread executes after the loop has "finished". The solution: List<Thread> threads = new List<Thread>(); for (int i = 0; i < 2; i++) { int j=i; // THIS IS THE CHANGE Thread t = new Thread(delegate() { Debug.Assert(j != 2); // AND THIS }); t.Start(); threads.Add(t); } foreach (Thread thread in threads) thread.Join(); There's a new instance of "j" each time we go round the loop. |
#13
| |||
| |||
|
|
Could you explain, what is the problem with having to introduce j as a temp. variable? |
|
What is the problem with the original code (without introduction of temp. variable inside the loop)? |
#14
| |||
| |||
|
#15
| |||
| |||
|
|
Could you explain, what is the problem with having to introduce j as a temp. variable? What is the problem with the original code (without introduction of temp. variable inside the loop)? |
#16
| |||
| |||
|
|
Pete, Thanks for your reply. Pardon my ignorance - but it outputs the same thing on the machine that I am on. |
|
If that's the case, how to really see them outputting different results (in order to produce the bug)? |
|
Also, in real life case -how to make sure that problems like this dont pass by & get caught when in production? |
#17
| |||
| |||
|
#18
| |||
| |||
|
#19
| |||
| |||
|
|
I'm not sure that Sleep nor a loop would help here - really I suspect all you need is: |
#20
| |||
| |||
|
|
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 |
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
| |