Thread safe DateTime update using Interlocked.*

前端 未结 4 1034
生来不讨喜
生来不讨喜 2021-02-12 22:40

Can I use an Interlocked.* synchronization method to update a DateTime variable?

I wish to maintain a last-touch time stamp in memory. Multiple http threads will update

4条回答
  •  春和景丽
    2021-02-12 23:11

    Yes, you can do this. Your biggest problem may be that DateTime.Ticks only has a resolution of ~20 ms. So it doesn't really matter if you keep a DateTime last or a long ticks variable. But since there is no overload of Exchange for DateTime, you need to use long.

提交回复
热议问题