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

后端 未结 5 505
误落风尘
误落风尘 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:30

    No, you have it backwards, data is accessible as long as it is still in scope.

    You need to guard against the opposite problem, two threads accessing the same data at the same time, which is called a race condition. You can use a synchronization technique like lock to prevent this from happening, but if used incorrectly it can lead to deadlock.

    Read C# Threading in .NET for a tutorial.

提交回复
热议问题