Re: Is this thread safe?

From: Willy Denoyette [MVP] (willy.denoyette_at_pandora.be)
Date: 12/21/04


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.



Relevant Pages

  • Re: multithreaded tcp/ip monitoring application
    ... Passing a copy of a reference type variable just passes the reference, and so mutations in the instance can still be seen by multiple threads. ... The string type is a reference type, but a string instance is immutable and the string class is thread-safe. ... the variable itself is mutable and that's why you need the synchronization. ... But, and this is very important, the "str" variable itself is not referenced by the invoked delegate. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Threads - why isnt a whole object locked when ...?
    ... All of your questions are answerable by pointing out that you have an incorrect mental model of what it means to "lock" something. ... That is, while it's entirely sensible to people who are familiar with standard thread synchronization techniques, in reality it's not actually locking anything. ... Instead, it's using the reference as a sort of traffic signal for other threads, which are cooperating, to respect. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: string as lock key?
    ... you never know who else might have a reference to the same ... String s2 = new ... I'm still debating if you really need a lock here. ... multiple requests come in and don't find an entry in the cache. ...
    (microsoft.public.dotnet.framework)
  • Re: string as lock key?
    ... you never know who else might have a reference to the same ... String s2 = new ... I'm still debating if you really need a lock here. ... multiple requests come in and don't find an entry in the cache. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Threadsafe value types
    ... string is created. ... This means a new reference is generated. ... the lock. ... > If a value type is immutable, I guess it's threadsafe to read it? ...
    (microsoft.public.dotnet.languages.csharp)