Home All Groups Group Topic Archive Search About

Is this static method threadsafe?

Author
13 Jan 2006 10:51 PM
Diffident
Hello All,

Following is a static method. Can you please tell me if this is
threadsafe...why? If it is not threadsafe...why?

Thank you.

public static string GetCachedApplicationWideObject(string HashtableKey)
        {
            //Cache["DefaultSampleQPage"] holds the hashtable
            if(Cache["DefaultSampleQPage"]==null)           
            {               
                CacheApplicationWideObjects();
                _sHTable = ((Hashtable)Cache["DefaultSampleQPage"]);   
                return ((string)HTable[HashtableKey]);   
            }
            else
            {               
                _sHTable = ((Hashtable)Cache["DefaultSampleQPage"]);
                return ((string)HTable[HashtableKey]);
            }           
        }

Author
13 Jan 2006 11:01 PM
Diffident
I forgot to add this information:

_sHTable is a static variable which is like a const variable for the entire
application; so no problem if two threads access that information at the same
time.

But my only question is if the parameter 1 is passed by thread 1 and
parameter 2 is passed by thread 2 is there any possibility that thread 1 will
be returned thread 2's result and vice versa?

Thanks

Show quoteHide quote
"Diffident" wrote:

> Hello All,
>
> Following is a static method. Can you please tell me if this is
> threadsafe...why? If it is not threadsafe...why?
>
> Thank you.
>
> public static string GetCachedApplicationWideObject(string HashtableKey)
>         {
>             //Cache["DefaultSampleQPage"] holds the hashtable
>             if(Cache["DefaultSampleQPage"]==null)           
>             {               
>                 CacheApplicationWideObjects();
>                 _sHTable = ((Hashtable)Cache["DefaultSampleQPage"]);   
>                 return ((string)HTable[HashtableKey]);   
>             }
>             else
>             {               
>                 _sHTable = ((Hashtable)Cache["DefaultSampleQPage"]);
>                 return ((string)HTable[HashtableKey]);
>             }           
>         }
Are all your drivers up to date? click for free checkup

Author
14 Jan 2006 12:51 AM
Bruce Barker
no its not threadsafe. you do not serialize access to the hashtable, so race
condiditons can occur. also two threads can run CacheApplicationWideObjects
at the same time (is it threadsafe?)

the treading issue you worried about should not be a problem if
CacheApplicationWideObjects is threadsafe.

-- bruce (sqlwork.com)




Show quoteHide quote
"Diffident" <Diffid***@discussions.microsoft.com> wrote in message
news:26E5BDB9-24C6-48D9-9B77-C0CE81B6E380@microsoft.com...
>I forgot to add this information:
>
> _sHTable is a static variable which is like a const variable for the
> entire
> application; so no problem if two threads access that information at the
> same
> time.
>
> But my only question is if the parameter 1 is passed by thread 1 and
> parameter 2 is passed by thread 2 is there any possibility that thread 1
> will
> be returned thread 2's result and vice versa?
>
> Thanks
>
> "Diffident" wrote:
>
>> Hello All,
>>
>> Following is a static method. Can you please tell me if this is
>> threadsafe...why? If it is not threadsafe...why?
>>
>> Thank you.
>>
>> public static string GetCachedApplicationWideObject(string HashtableKey)
>> {
>> //Cache["DefaultSampleQPage"] holds the hashtable
>> if(Cache["DefaultSampleQPage"]==null)
>> {
>> CacheApplicationWideObjects();
>> _sHTable = ((Hashtable)Cache["DefaultSampleQPage"]);
>> return ((string)HTable[HashtableKey]);
>> }
>> else
>> {
>> _sHTable = ((Hashtable)Cache["DefaultSampleQPage"]);
>> return ((string)HTable[HashtableKey]);
>> }
>> }
Author
14 Jan 2006 12:46 AM
Peter Bromberg [C# MVP]
If all you are ever doing is reading from the Cache, i fail to see where the
problem lies, because the data will always be the same.
It's when you might have another thread that attempts to WRITE to the same
Cache item that you could run into issues.

Take a look at the lock keyword. Better yet, read MVP Jon Skeet's articles
on the subject:

http://www.yoda.arachsys.com/csharp/threads/

Peter
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com




Show quoteHide quote
"Diffident" wrote:

> Hello All,
>
> Following is a static method. Can you please tell me if this is
> threadsafe...why? If it is not threadsafe...why?
>
> Thank you.
>
> public static string GetCachedApplicationWideObject(string HashtableKey)
>         {
>             //Cache["DefaultSampleQPage"] holds the hashtable
>             if(Cache["DefaultSampleQPage"]==null)           
>             {               
>                 CacheApplicationWideObjects();
>                 _sHTable = ((Hashtable)Cache["DefaultSampleQPage"]);   
>                 return ((string)HTable[HashtableKey]);   
>             }
>             else
>             {               
>                 _sHTable = ((Hashtable)Cache["DefaultSampleQPage"]);
>                 return ((string)HTable[HashtableKey]);
>             }           
>         }

Bookmark and Share