HighTechTalks DotNet Forums  

Shared Method and Thread Safe

Dotnet Framework (Performance) microsoft.public.dotnet.framework.performance


Discuss Shared Method and Thread Safe in the Dotnet Framework (Performance) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Robert Strickland
 
Posts: n/a

Default Shared Method and Thread Safe - 11-23-2005 , 01:52 PM






I have a shared/static method that access an object from the AppDomain. If the object is not cached, the method creates an object reference and caches the reference into the Appdomain. Multiple threads will use this method. I would like to make the method thread-safe by sychronizing specific code sections in the method. Is this necessary and, if so, what is the best way to sync critical code sections in a static method?

Reply With Quote
  #2  
Old   
Robert Strickland
 
Posts: n/a

Default Re: Shared Method and Thread Safe - 11-24-2005 , 10:03 AM






Thanks.
"Gabriel Lozano-Morán" <glozano (AT) no-spam (DOT) org> wrote

You can synchronize using either the lock statement or by the System.Threading.Monitor class

Class1 is a reference type by the way...

lock(typeof(Class1))
{
// Do something
}

or

Monitor.Enter(typeof(Class1));
try
{
// do something
}
finally
{
Monitor.Exit();
}


Gabriel Lozano-Morán

Reply With Quote
  #3  
Old   
Jon Skeet [C# MVP]
 
Posts: n/a

Default Re: Shared Method and Thread Safe - 11-24-2005 , 12:46 PM



=?Windows-1252?Q?Gabriel_Lozano-Mor=E1n?= <glozano (AT) no-spam (DOT) org> wrote:
Quote:
You can synchronize using either the lock statement or by the System.Threading.Monitor class

Class1 is a reference type by the way...

lock(typeof(Class1))
Note, however, that locking on a publicly available reference is a bad
idea - see http://www.pobox.com/~skeet/csharp/t...ckchoice.shtml

--
Jon Skeet - <skeet (AT) pobox (DOT) com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.