Re: static constructor and multithreading

From: Jon Skeet [C# MVP] (skeet_at_pobox.com)
Date: 05/08/04


Date: Sat, 8 May 2004 07:03:15 +0100

Marek <marek.stachowicz@sbcglobal.net> wrote:
> I was not precise enough in the question 4. Of course, other resources may
> have to be protected against concurrent access. I meant that locking on the
> type class instance inside the static constructor does not make any sense.
> Is that correct?

No - because as Nicholas said, other classes may be using the type for
locking as well. The things that the static constructor does may need
to execute separately from anything else which would lock on the type.
 
> As far as I have understood your answer to the helper question, CLR provides
> some kind of protection against letting other static methods get started
> before the static constructor ends its execution. Is that what you said?

Yes, with some conditions. See section 9.5.3 of partition 1 of the ECMA
spec for more details.

-- 
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too