Re: Multi-Threading Question
- From: "Richard Blewett [DevelopMentor]" <richardb@xxxxxxxxxxxxxxxxx>
- Date: Sun, 11 Sep 2005 10:01:11 -0700
Hmmmm - be careful, in C# 2.0 anonymous delegates change the landscape a bit. Variables that appear to be local turn out to be allocated on the heap.
void DoIt()
{
int x = 0;
MyDel d = delegate
{
for( int i = 0; i < 2000; i++ )
{
x += i;
}
};
IAsyncResult ar = d.BeginInvoke(null, null);
x++;
d.EndInvoke(ar);
}
In x is now being updated from 2 threads and is (under the covers) being allocated on the head in a temporary object.
Regards
Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk
Local variables are always thread-safe, since they are not shared among
threads. This is because each thread has its own stack (where the local
variables are stored).
.
- References:
- Re: Multi-Threading Question
- From: -
- Re: Multi-Threading Question
- Prev by Date: Re: Multi-Threading Question
- Next by Date: Re: Multi-Threading Question
- Previous by thread: Re: Multi-Threading Question
- Next by thread: Re: SystemColors.ActiveCaption ?
- Index(es):