Timer (System.Threading) thread safety

后端 未结 2 707
离开以前
离开以前 2021-01-23 00:24

Does anyone know if this code would be thread safe, or do I have to use lock when calling timer2.Change?

Timer timer1 = new Timer(timerCallback1);
Timer timer2 =         


        
2条回答
  •  广开言路
    2021-01-23 00:40

    It's "thread-safe" in the sense that the call to Change won't actually corrupt the timer.

    However, it's not "thread-safe" in the sense that you definitely have a race condition (it's not possible to ensure that timerCallback2 isn't running when you're in DoStuff).

提交回复
热议问题