Do I need to lock or mark as volatile when accessing a simple boolean flag in C#?

前端 未结 5 1623
余生分开走
余生分开走 2020-11-28 23:38

Lets just say you have a simple operation that runs on a background thread. You want to provide a way to cancel this operation so you create a boolean flag that you set to t

5条回答
  •  心在旅途
    2020-11-29 00:00

    Look up Interlocked.Exchange(). It does a very fast copy into a local variable which can be used for comparison. It is faster than lock().

提交回复
热议问题