Do properties have volatile effect?

后端 未结 6 1806
無奈伤痛
無奈伤痛 2021-02-19 10:54

In the code below will read1 be always equal to read2, provided property Flag can be changed from other threads? Concern here is that

6条回答
  •  渐次进展
    2021-02-19 11:15

    To sum up the other replies, in this situation, what happens to the values of the two variables after the code is executed can't be predicted. Both because the CLR and compiler are largely black boxes to us, and because any sort of prediction regarding the outcome of a race condition is really a gamble that's bound to be wrong at some point.

    Regardless, you cannot write code of this sort in a multi-threaded environment.

提交回复
热议问题