Actually, I have an example fetched from the tracer routines of Duwamish where the static constructor makes sure no other thread is excecuting the samme constructor. Check out the code below.
static TraceHelper() { //Get the class object in order to take the initialization lock Type myType = typeof(TraceHelper); //Protect thread locks with Try/Catch to guarantee that we let go of the lock. try { //See if anyone else is using the lock, grab it if they//re not if (!Monitor.TryEnter(myType)) { //Just wait until the other thread finishes processing, then leave if // the lock was already in use. Monitor.Enter(myType); return; } // Do some init of static member variables } finally { //Remove the lock from the class object Monitor.Exit(myType); } } - Peter Lillevold -----Original Message----- From: Stefan Avramtchev [mailto:[EMAIL PROTECTED]] Sent: 18. juni 2002 14:47 To: [EMAIL PROTECTED] Subject: Re: [ADVANCED-DOTNET] Singleton pattern I think the type contructors is thread safe. How otherwise is the framework guarding this rule if not by locking some internal object (not the being-initialised class ofcourse) in order to guarantee that only one thread runs inside the type contructor? stef On Fri, 14 Jun 2002 15:01:22 -0700, Keith Hill <[EMAIL PROTECTED]> wrote: >>> >"The static constructor for a class executes at most one time >>> >during a single program instantiation" > >The more I think about it, the statement above does not imply thread- >safety, at least not to me. Can someone confirm that static >constructors are indeed thread-safe? > >-- >Keith > You can read messages from the Advanced DOTNET archive, unsubscribe from Advanced DOTNET, or subscribe to other DevelopMentor lists at http://discuss.develop.com. You can read messages from the Advanced DOTNET archive, unsubscribe from Advanced DOTNET, or subscribe to other DevelopMentor lists at http://discuss.develop.com.