![]() | |
![]() |
| | Thread Tools | Search this Thread | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
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? |
#3
| |||
| |||
|
|
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 |
#4
| |||
| |||
|
|
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 |
#5
| |||
| |||
|
|
Even though it does deal with concurrency, I wouldn't file this under a concurrency issue, per se. The error comes from using an anonymous method in the loop and capturing the variable "i". Because it is used in the anonymous method (and there is only one instance of it created), when the loop exits, i is equal to 2. Not all the threads have started up by this point, and then by the time that they do, the assertion fails. -- - Nicholas Paldino [.NET/C# MVP] - mvp (AT) spam (DOT) guard.caspershouse.com "Chris Mullins [MVP - C#]" <cmullins (AT) yahoo (DOT) com> wrote in message news:e6FVjpmFIHA.2268 (AT) TK2MSFTNGP02 (DOT) phx.gbl... 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 |
#6
| |||
| |||
|
|
Chris Mullins [MVP - C#] 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? Define "bug". Assuming your main thread doesn't get preempted while creating the threads, both threads are going to fail the assertion. And since the main thread is doing so little, it is in fact likely to get all the way to the first call to Join() before another thread gets to run. But is that a bug? The Debug class is thread-safe, so having two threads concurrent fail an assertion shouldn't cause a problem in and of itself. Pete |
#7
| |||
| |||
|
|
[...] It was also confusing, as I *expected* the variable passed into the closure to be passed by value (it's an int, afterall). I expected to get the same behavior as if I had passed in a constant. The fact that C# boxed the variable, passed in a reference to it, and then later checked it after the for-loop had completed (and the original variable was out of scope) and got the "illegal" variable, really was amusing... |
#8
| |||
| |||
|
|
Well, it's a bug in that the code doesn't do what was originally intended. Instead an "impossible" assertion fires, and the developer(s) get a very confused look on their faces for a few minutes... |

|
Your explination is dead on, but the boxing / byref behavior of the variable passed into the Closure is what made this code behavie in a way other than was expected. I was expecting the int (a value type) to be passed in by value, and therefore not change... |

#9
| |||
| |||
|
|
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(); |
#10
| |||
| |||
|
|
Chris Mullins [MVP - C#] wrote: [...] It was also confusing, as I *expected* the variable passed into the closure to be passed by value (it's an int, afterall). I expected to get the same behavior as if I had passed in a constant. The fact that C# boxed the variable, passed in a reference to it, and then later checked it after the for-loop had completed (and the original variable was out of scope) and got the "illegal" variable, really was amusing... Maybe someone who has more intimate knowledge can comment, but AFAIK this isn't a case of the value type being boxed. If it were, you wouldn't have had the problem, since the boxing still preserves the value as it was at the moment of boxing, not referencing the variable itself. Rather, by capturing the variable in the anonymous method, what is being used in the method is the variable itself. That's why any change to the variable that happens before the code that uses it is executed is seen when that code is executed. The key here is the "capturing" behavior, and I believe that has nothing to do with boxing. I think the variable capturing that happens with anonymous methods is pretty cool, actually. But I agree it can lead to some non-obvious results when one is not aware that the capturing is going on, and what the capturing does. Pete |
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
| |