Thread Safe Public variables

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

jazzcatoneAsked:
Who is Participating?
 
käµfm³d 👽Connect With a Mentor 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.
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
Basically no.
You have no control if another thread changes the value whilst a different thread is using it.
0
 
käµfm³d 👽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.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
jazzcatoneAuthor Commented:
Even if its from a different session ???
0
 
käµfm³d 👽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.
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
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.
0
 
jazzcatoneAuthor 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.
0
 
käµfm³d 👽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?
0
 
jazzcatoneAuthor 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.
0
 
jazzcatoneAuthor Commented:
Thank you
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
@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; )
0
 
jazzcatoneAuthor Commented:
Not sure what your question is...you're saying somehow j ended up being equal to 42 ?
0
 
käµfm³d 👽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.
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
@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.)
0
 
käµfm³d 👽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.
0
All Courses

From novice to tech pro — start learning today.