473,394 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

thread safety / static method

Hi, I am worried about thread-safety in the following code.
If two threads call GetObject() is it possible they both create a new
AlphaContext object and add it to HttpContext.Items?

Can I avoid this with a lock? (I assum I would need a "static" object to
lock on?)
public class ContextFactory
{
private const string OBJECT_KEY = "AlphaSContext";

private ContextFactory()
{
}

public static IContext GetObject()
{
HttpContext httpContext = HttpContext.Current;
IContext context = null;

if (httpContext.Items.Contains(OBJECT_KEY))
{
context = (IContext)httpContext.Items[OBJECT_KEY];
}
else
{
context = new AlphaContext();
httpContext.Items.Add(OBJECT_KEY, context);
}

return (IContext)context;
}
}
Thanks, Peter
Dec 19 '07 #1
2 1949
If two threads call GetObject() is it possible they both create a new
AlphaContext object and add it to HttpContext.Items?
If the two threads are executing in the same http-context, then yes
this is a possibility. HttpContext.Current returns the http-context
for the current thread - it isn't a single static value so you don't
need to worry about *all* threads; just those that you have created
(directly or indirectly) relating to the same http-context.

I don't know what the AlphaContext represents here (and what the cost
of the object is), but a pragmatic option might be to simply ensure
there is a valid context before you start spawning threads?

A static lock would have the side-effect of serializing *all* threads
- not just those relating to the same http-context; I would be
cautious about locking on the httpContext itself, though, simply
because you don't know what else might be locking on it.

Marc
Dec 19 '07 #2
On Dec 19, 9:14 am, Peter K <xdz...@hotmail.comwrote:
Hi, I am worried about thread-safety in the following code.
If two threads call GetObject() is it possible they both create a new
AlphaContext object and add it to HttpContext.Items?
It would only be an issue if two threads were using the same
HttpContext.

While that's possible, it's unlikely - and you should know about it if
your code might execute in that fashion.

Jon
Dec 19 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jonathan Burd | last post by:
Greetings everyone, Here is a random string generator I wrote for an application and I'm wondering about the thread-safety of this function. I was told using static and global variables cause...
11
by: dee | last post by:
OleDbCommand class like many .NET classes has the following description in its help file: "Thread Safety Any public static (Shared in Visual Basic) members of this type are safe for...
7
by: Chad Zalkin | last post by:
We are evaluating some old code that was written as part of our math library. This code uses some optimizations that I'm not sure are necessary or safe, but is a source of debate between my...
4
by: Warren Sirota | last post by:
Hi, I've got a method that I want to execute in a multithreaded environment (it's a specialized spider. I want to run a whole bunch of copies at low priority as a service). It works well running...
10
by: Paul | last post by:
Hi all, All of the classes in my DAL are static, with constants defining the stored procedures and parameters. I've been having some problems with my site which makes me wonder if there's a...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.