c# - Volatile keyword usage vs lock

前端 未结 5 1222
予麋鹿
予麋鹿 2020-11-28 04:45

I\'ve used volatile where I\'m not sure it is necessary. I was pretty sure a lock would be overkill in my situation. Reading this thread (Eric Lippert comment) make me anxio

5条回答
  •  我在风中等你
    2020-11-28 05:08

    The comparison and assignment in your InternalSave() method would not be thread safe with or without the volatile keyword. If you would like to avoid using locks, you could use the CompareExchange() and Increment() methods in your code from the framework's Interlocked class.

提交回复
热议问题