469,600 Members | 2,326 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,600 developers. It's quick & easy.

Is this static method threadsafe?

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]);
}
}
Jan 13 '06 #1
3 1997
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]);
}
}

Jan 13 '06 #2
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


"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]);
}
}

Jan 14 '06 #3
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)


"Diffident" <Di*******@discussions.microsoft.com> wrote in message
news:26**********************************@microsof t.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]);
}
}

Jan 14 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

25 posts views Thread by Sahil Malik [MVP] | last post: by
2 posts views Thread by David | last post: by
11 posts views Thread by Dave | last post: by
7 posts views Thread by Jon Vaughan | last post: by
3 posts views Thread by David Veeneman | last post: by
3 posts views Thread by Adam | last post: by
12 posts views Thread by chandu | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.