Solved

Thread Safe Public variables

Posted on 2014-02-26
15
341 Views
Last Modified: 2014-03-04
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

0
Comment
Question by:jazzcatone
  • 6
  • 5
  • 4
15 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39889001
Basically no.
You have no control if another thread changes the value whilst a different thread is using it.
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 39889021
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
 

Author Comment

by:jazzcatone
ID: 39889022
Even if its from a different session ???
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 39889028
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
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39889074
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
 

Author Comment

by:jazzcatone
ID: 39889077
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
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 39889092
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:jazzcatone
ID: 39889113
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
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39889132
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
 

Author Closing Comment

by:jazzcatone
ID: 39895900
Thank you
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39896903
@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
 

Author Comment

by:jazzcatone
ID: 39900414
Not sure what your question is...you're saying somehow j ended up being equal to 42 ?
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 39900624
@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
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39902726
@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
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 39903773
@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

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now