HighTechTalks DotNet Forums  

Spot the Bug: Fun Concurrency Bug

Dotnet Framework microsoft.public.dotnet.framework


Discuss Spot the Bug: Fun Concurrency Bug in the Dotnet Framework forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #21  
Old   
Marc Gravell
 
Posts: n/a

Default Re: Spot the Bug: Fun Concurrency Bug - 10-25-2007 , 06:18 AM






Quote:
As a follow-on question... where does variable i 'live'. It's local
and
a value type, so lives on the local stack...
Nope, it doesn't live on the stack - it is "captured", so it lives as
a field on an object that the C# compiler creates for you. The rules
for the scoping of each "captured" variable is quite tricky... but but
*conceptually* what we are talking about (without using captures)
would be comparable to the following (although the compiler implements
it differently):

class SomeObject {
int i;
void SomeMethod() {
Debug.Assert(i != 2);
}
}
....
SomeObject obj = new SomeObject(); // obj instance is managed
for(int tmp = 0; tmp < 2; tmp ++) { // tmp lives on the stack
obj.i = tmp; // i lives on obj
Thread t = new Thread(obj.SomeMethod);
}

note that there is only a single "obj", and hence all share an "i".
Now compare to the version with a "j" introduced (see my other post in
this topic):

class SomeOtherObject {
int j;
void SomeOtherMethod() {
Debug.Assert(j != 2);
}
}
....
for(int i = 0; i < 2; i++) { // i lives on the stack
SomeOtherObject obj = new SomeOtherObject(); // obj instance is
managed
obj.j = i; // j lives on obj
Thread t = new Thread(obj.SomeOtherObject);
}

in this latter case, each loop iteration gets a different object, and
hence a different "j"

Please note: I have simplified the behavior to make a simple example.

Marc




Reply With Quote
  #22  
Old   
Jon Skeet [C# MVP]
 
Posts: n/a

Default Re: Spot the Bug: Fun Concurrency Bug - 10-25-2007 , 06:25 AM






On Oct 25, 10:47 am, AJ <no... (AT) nowhere (DOT) com> wrote:
Quote:
As a follow-on question... where does variable i 'live'. It's local and
a value type, so lives on the local stack...
Well, it's only *sort* of a local variable. It's actually a captured
variable, and will live on the heap. It's the fact that it's captured
that makes the whole thing confusing.

Jon



Reply With Quote
  #23  
Old   
Mads Bondo Dydensborg
 
Posts: n/a

Default Re: Spot the Bug: Fun Concurrency Bug - 10-29-2007 , 03:53 AM



Chris Mullins [MVP - C#] wrote:

Quote:
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...
Actually, this functionality is much more useful. Besides the explanations
given here, this page may be interessting to you:

http://en.wikipedia.org/wiki/Closure_(computer_science)

Regards,

Mads

--
Med venlig hilsen/Regards

Systemudvikler/Systemsdeveloper cand.scient.dat, Ph.d., Mads Bondo
Dydensborg
Dansk BiblioteksCenter A/S, Tempovej 7-11, 2750 Ballerup, Tlf. +45 44 86 77
34


Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.