By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,052 Members | 1,303 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,052 IT Pros & Developers. It's quick & easy.

threading and variable scope question

P: n/a
If I have a class C that contains a private variable (property) V and
private function T and T is executed within C as a separate thread, does
thread T have access to private variable (property) V in the instance of
class C? It appears to but another person has suggested that this is
dangerous and that global variables should be defined in a separate class
with the static type modifer.
Sep 22 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Mark,

The method that is executed doesn't have accessibility changed because
it is running in another thread. So if your thread method T is indeed on C,
then it should have access to all members of that instance of C (if it is an
instance method).

The reason this is dangerous is that if the method T is not the only
method accessing V (which it most likely isn't, otherwise it would be a
variable in the method itself), you need to synchronize access to it. The
easiest way to do this is through the lock statement.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Mark Huebner" <mh******@bostwicklaboratories.comwrote in message
news:uL**************@TK2MSFTNGP04.phx.gbl...
If I have a class C that contains a private variable (property) V and
private function T and T is executed within C as a separate thread, does
thread T have access to private variable (property) V in the instance of
class C? It appears to but another person has suggested that this is
dangerous and that global variables should be defined in a separate class
with the static type modifer.

Sep 22 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.