A thread safety question

S

Simon Harvey

Hi all.

I have a page and a control that both raise the same issue:

I have the following code:

private bool isInCreateMode = false;

public bool IsInCreateMode{
get{
...
}
set{
...
}
}

My question is, given that this variable is global ie, outside the context
of a given method call, is it thread safe?

I know that in java, declaring a variable globaly like that can cause
problems.

Do I need to worry?

Thanks

Simon
 
R

Rick Strahl [MVP]

Hi Simon,

If the control sits on a Web Page it's probably safe, unless you have
static/shared members in the control.

Remember that a control is an object that gets instantiated by the HTTP
Runtime on a particular thread. One thread / one Page typically (unless you
run async requests or fire your own threads). Another thread can't easily
call into this page and any control on the page by default is private
anyway, so unless you're doing somehting tricky with state (sessions,
Application object, or caching your data in a static member somewhere) in
your application you won't have any problems.

You do need to watch out for static/shared members in components or
controls. Those can easily cause problems if not properly managed...

+++ Rick ---

--

Rick Strahl
West Wind Technologies
http://www.west-wind.com/
http://www.west-wind.com/weblog/
http://www.west-wind.com/wwThreads/
 
S

Simon Harvey

Hi there Rick,

Thanks for your reply. I just want to check one thing. You talked about
controls on a page there which was very helpful.

I also declare global variables and properties on pages themselves. I'm
guessing that that will be fine as well given that you said one page/one
thread

I just wanted to make sure before i go and arse my companies website
entirely!

:)

Simon
 
A

Alvin Bruney [MVP]

global variables are not thread safe. You need to protect them. It works the
same in c# as in java or most other languages for that matter.
 

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

Similar Threads


Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,835
Latest member
KetoRushACVBuy

Latest Threads

Top