Why can a local variable be accessed in another thread created in the same class?

后端 未结 5 494
误落风尘
误落风尘 2021-01-31 11:38

I couldn\'t really find anything on this exact topic, so please lead me toward the right direction, if a question already exists.

From what I have learned about .NET, it

5条回答
  •  走了就别回头了
    2021-01-31 12:09

    From what I have learned about .NET, it is not possible to access variables across different threads (please correct me if that statement is wrong, it's just what I have read somewhere).

    That is not correct. A variable can be accessed from anywhere that it is in scope.

    You need to exercise caution when accessing the same variable from multiple threads because each thread can act on the variable at a non-deterministic time leading to subtle, hard-to-resolve bugs.

    There is an outstanding website that covers threading in .NET from the basics to advanced concepts.

    http://www.albahari.com/threading/

提交回复
热议问题