Re: Is this thread safe?
From: Willy Denoyette [MVP] (willy.denoyette_at_pandora.be)
Date: 12/21/04
- Next message: vijay: "To add a taskbar to a C# application"
- Previous message: Kevin Yu [MSFT]: "RE: How can I use an enum as the datasource for a combobox?"
- In reply to: Richard Blewett [DevelopMentor]: "Re: Is this thread safe?"
- Next in thread: Richard Blewett [DevelopMentor]: "Re: Is this thread safe?"
- Reply: Richard Blewett [DevelopMentor]: "Re: Is this thread safe?"
- Messages sorted by: [ date ] [ thread ]
Date: Tue, 21 Dec 2004 11:27:22 +0100
"Richard Blewett [DevelopMentor]" <richardb@NOSPAMdevelop.com> wrote in
message news:e4jhxqu5EHA.1204@TK2MSFTNGP10.phx.gbl...
> Actually just rechecked something and static members don't appear to work.
> I was using Environment.NewLine as a ref type to lock which did work. But
> then after some thought I realised this is simply an interned string being
> returned so it proves only that interned strings work. However, if I
> switch to Environment.OSVersion which is a demand allocated reference
> type, I get no synchronization.
>
> My blog post about this is here
>
> http://www.dotnetconsult.co.uk/weblog/PermaLink.aspx/362caf16-e573-4cb1-ae01-79704555d2b4
>
> Regards
>
> Richard Blewett - DevelopMentor
> http://www.dotnetconsult.co.uk/weblog
> http://www.dotnetconsult.co.uk
>
Richard,
We have to make a clear distinction between:
1 - domain-neutral assemblies and how they are shared between application
domains and
2 - object instances and how they are allowed to cross application domains
boundaries.
Say you have two application domains that use the typeof operator to get the
System.Type object for a type that was loaded from a domain-neutral
assembly, here typeof will return a direct reference to a single type and
you'll get cross-application domain synchronization when putting a lock on
it .
object o = typeof(string); // System.String is loaded from a domain-neutral
assembly mscorlib.dll
as used in the sample you posted [1] is a perfect sample, and that is what I
was referring to in my previous post.
Note that it doesn't matter if the reference is a static or an instance
field, static fields are always per-application domain (except RVA
static's), it's the type it references that matters (see later).
Object instances however, are only allowed to be shared between application
domains, when they:
- are of a type loaded as domain-neutral and
- when they marshal-by-bleed or marshal as identity-preserving by-value.
For instance if you make o a string reference;
static string o = "a string";
you will end with two static references directly pointing to the single
instance string (marshal-by-bleed) and get cross-domain synchronization when
locking the instance.
The only other type (AFAIK) that is allowed to marshal-by-bleed is the
System.Threading.Thread.
That's why Environment.OSVersion doesn't "synchronize", it is not a
System.String nor a System.Threaing.Thread type so it doesn't
marshal-by-bleed.
Willy.
- Next message: vijay: "To add a taskbar to a C# application"
- Previous message: Kevin Yu [MSFT]: "RE: How can I use an enum as the datasource for a combobox?"
- In reply to: Richard Blewett [DevelopMentor]: "Re: Is this thread safe?"
- Next in thread: Richard Blewett [DevelopMentor]: "Re: Is this thread safe?"
- Reply: Richard Blewett [DevelopMentor]: "Re: Is this thread safe?"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|