Thread Safe Public variables

jazzcatone
jazzcatone used Ask the Experts™
on
I have a page level variable marked as public. It is called claimid. It  is used as an input parameter a lot in my codebehind for pulling back information. (see this below) My question: Is this thread safe?

I ask because we have had issues in the past with public static variables being used and the wrong information showing up when multiple users are accessing the page. Is there any risk of the same thing happening with the below scenario? Any advice would be greatly appreciated.

Jason

 public partial class ClaimUpdate : Holding
    {
            
            public int claimid;

          protected void Page_Load(object sender, EventArgs e)
          {
                claimid = Convert.ToInt32(Request.Querystring["claimid"]);

               // use claim id all over the place in different events once retrieved


          }
    }

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
AndyAinscowFreelance programmer / Consultant

Commented:
Basically no.
You have no control if another thread changes the value whilst a different thread is using it.
Most Valuable Expert 2011
Top Expert 2015

Commented:
First, why are you creating public variables to begin with? This is a code smell. Second, I agree with Andy:  You need to control access to this variable, and you would typically do this by exposing a property (or method) that handles such. By adding the property, you can include thread-syncrhonization logic that would help protect that variable.

Author

Commented:
Even if its from a different session ???
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

Most Valuable Expert 2011
Top Expert 2015

Commented:
If you have data that is shared, then you must protect it if you want to employ threading. If two or more threads have access to the same piece of data, then it needs to be protected.
AndyAinscowFreelance programmer / Consultant

Commented:
Basically you can read from multiple threads without a problem.  The problem is when you can write from multiple threads.  As soon as that can happen then you need to control things else GIGO (Garbage In Garbage Out) happens.

Author

Commented:
Well I use this a lot. So the thinking was rather than requesting the querystring value 5,000 times that this would be more efficient as I only need to dedicate a spot in memory for this once and then refer to it.
Most Valuable Expert 2011
Top Expert 2015

Commented:
I would modify Andy's most recent assertion slightly:  Even if you write from only one thread, reading from multiple threads could still lead to race conditions where one (or more) thread read different data than the remaining threads.

If this is ASP.NET related, then where are the multiple threads coming into play? Are you referring solely to the ASP.NET worker threads, or are you yourself creating new threads?

Author

Commented:
I guess it would be worker threads. I'm not doing any multi threading in code per say. This would just be purely how IIS serves up the information to the user based on the given claimid.
Most Valuable Expert 2011
Top Expert 2015
Commented:
If you are referring to the worker threads, then your data should already be safe. If you are not marking the variable as static, then there should be no issue.

Author

Commented:
Thank you
AndyAinscowFreelance programmer / Consultant

Commented:
@kaufmed
>>If you are referring to the worker threads, then your data should already be safe.

Question:
In the main app, a public variable x = 1
this is in a worker thread
long j = 0;
for(i=0; i<1000000; i++)
{
  //do something
  j = j + MainApp.x
}

the question is what is the value of j after the worker thread loop exits ?
(ps. At some unknown time during the execution of the loop something else ran the following code MainApp.x = 42; )

Author

Commented:
Not sure what your question is...you're saying somehow j ended up being equal to 42 ?
Most Valuable Expert 2011
Top Expert 2015

Commented:
@AndyAinscow

By "worker threads" I am referring to the threads that ASP.NET itself spawns up. So far as I know, each request is served by only one thread--unless the programmer spawns an additional thread; hence one of my earlier questions. There should be no cross-threading going on for any one request. If you know otherwise, I will gladly recant.
AndyAinscowFreelance programmer / Consultant

Commented:
@kaufmed.
This is the part of your comment that I'm not too happy with:
If you are referring to the worker threads, then your data should already be safe.
I still say this (page level public variable) is not thread safe.  Note this is not the same as the apparently similar but in reality rather different question 'Will this work reliably' - which is probably what the question should really be about.
I tend to agree with you that any worker threads spawned by ASP.NET won't have an effect, not because it is thread safe but because they just won't be spawned with a requirement on variables defined in the code.  (Otherwise that implies one of two things: a trivial way to make things thread safe that Microsoft aren't telling anyone about or an incredibly complex mechanism to wrap anything a programmer comes up with in a thread safe manner.  Occams razor makes me think threads just aren't being used.)


As an academic exercise it would be interesting to see if one could deliberately create a worker thread to mess about with a variable like this and see if it did create garbage occasionally.  (That would mean the 'will it work reliably' question also technically requires a no answer.)
Most Valuable Expert 2011
Top Expert 2015

Commented:
@Andy

Yes, I agree that they are not truly thread safe, and I did not intend to imply exactly that. More prudence in my wording is probably necessary.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial