Is this static method threadsafe?

G

Guest

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

Guest

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
 
G

Guest

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
 
B

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)




Diffident said:
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 said:
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]);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top