threading and variable scope question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mark Huebner

    threading and variable scope question

    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.


  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: threading and variable scope question

    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]
    - mvp@spam.guard. caspershouse.co m

    "Mark Huebner" <mhuebner@bostw icklaboratories .comwrote in message
    news:uLkUUEm3GH A.4588@TK2MSFTN GP04.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.
    >

    Comment

    Working...