Is this thread safe?

  • Thread starter Thread starter Dan Bass
  • Start date Start date
OK, yes I forgot about domain neutral loaded assemblies - after playing with this for a while to make sure I wasn't smoking ;-) it only works for the type object and any reference type static members for a type in a domain neutral assembly though. Instances of domain neutral types are not themselves domain neutral.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk


Richard Blewett said:
Nope, Monitors are AppDomain specific.If you want to perform cross
AppDomain (including cross process) you will need to use Mutexes, Events
and other such kernel objects

Richard,

IMO Monitors themselves are not Domain "specific", all depends on the type
you are taking a lock on. If you take a lock on a domain neutral type, you
will get a cross-domain lock, else you get a domain specific lock.

Willy.
 
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

OK, yes I forgot about domain neutral loaded assemblies - after playing with this for a while to make sure I wasn't smoking ;-) it only works for the type object and any reference type static members for a type in a domain neutral assembly though. Instances of domain neutral types are not themselves domain neutral.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk


Richard Blewett said:
Nope, Monitors are AppDomain specific.If you want to perform cross
AppDomain (including cross process) you will need to use Mutexes, Events
and other such kernel objects

Richard,

IMO Monitors themselves are not Domain "specific", all depends on the type
you are taking a lock on. If you take a lock on a domain neutral type, you
will get a cross-domain lock, else you get a domain specific lock.

Willy.
 
Richard Blewett said:
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.

There's one more test, however: test with a MarshalByRefObject, eg
Pens.Black.

I would have tried this with your test code, but it blew up with a
SerializationException for me (as posted).
 
Richard Blewett said:
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.
 
Thanks for the clarification Willy. I'd heard of marshal-by-bleed, it always sounded horrbly painful and I never dug into it ;-) but i know what it is. I'd never heard of "marshal as identity-preserving by-value", do you have any more info or a link to details?

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

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.
 
Back
Top