Re: int number = new int();
- From: Jon Skeet [C# MVP] <skeet@xxxxxxxxx>
- Date: Sun, 28 Sep 2008 09:12:06 +0100
Peter Duniho <NpOeStPeAdM@xxxxxxxxxxxxxxxx> wrote:
[...]
However, it can occasionally be an interesting way of thinking about
other aspects of behaviour. It's easy enough to see that if data is to
be shared between threads, it has to be on the heap - and conversely,
that if data is only on the stack, it's threadsafe. (Assuming no
*really* nasty hacks, admittedly.)
Well, that's interesting.
I'm still not sure I'd go around assuming that data on the stack is
threadsafe, but until you wrote the above, I had no idea that you weren't
allowed to use by-reference arguments in anonymous methods.
You can:
using System;
class Test
{
delegate void Foo(ref int i);
public static void Main()
{
Foo f = delegate (ref int x) { x = 10; };
int j = 0;
f(ref j);
Console.WriteLine(j);
}
}
Not sure what you meant, but the thread-safe aspect seems sensible to
me - if the data is on the stack, how can another thread get at it?
Note that I'm talking about the *actual data* here - if you happen to
have a reference to an object, then of course a different thread may be
able to modify that object's data if it's got a reference to it.
As for "*really* nasty hacks" go, I guess that depends on your
definition. All that's really necessary in order to get stack-stored data
from one thread to another is the use of unsafe code (i.e. pointers).
It's not like you have to write self-modifying code, or generate your own
IL, or otherwise do an end-around of the compiler.
Unsafe code at both sides (I suspect) - which means it would at least
be deliberate.
I'm of the general opinion that any use of unsafe code could be considered
"nasty", but I have to admit that not everyone agrees. And personally, I
think maybe unsafe code is simply "nasty", not necessarily "*really*
nasty".
So, what's _your_ definition of "*really* nasty hacks"? I assume it
includes the use of unsafe code. :)
Not necessarily - but anything which is deliberately making stack data
available to a different thread is really nasty (and brittle) IMO.
--
Jon Skeet - <skeet@xxxxxxxxx>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
.
- Follow-Ups:
- Re: int number = new int();
- From: Peter Duniho
- Re: int number = new int();
- References:
- int number = new int();
- From: Tony Johansson
- Re: int number = new int();
- From: Cor Ligthert [MVP]
- Re: int number = new int();
- From: Tony Johansson
- Re: int number = new int();
- From: Jon Skeet [C# MVP]
- Re: int number = new int();
- From: Tony Johansson
- Re: int number = new int();
- From: Jon Skeet [C# MVP]
- Re: int number = new int();
- From: Tony Johansson
- Re: int number = new int();
- From: Jon Skeet [C# MVP]
- Re: int number = new int();
- From: xcal
- Re: int number = new int();
- From: Jon Skeet [C# MVP]
- Re: int number = new int();
- From: Peter Duniho
- int number = new int();
- Prev by Date: Re: creating a _simple_ fine grained user based security system
- Next by Date: Re: about the string Class and the StringBuilder class
- Previous by thread: Re: int number = new int();
- Next by thread: Re: int number = new int();
- Index(es):
Relevant Pages
|