Non-reentrant timers

后端 未结 5 476
别那么骄傲
别那么骄傲 2020-12-11 04:30

I have a function that I want to invoke every x seconds, but I want it to be thread-safe.

Can I set up this behavior when I am creating the timer? (I don\'t

5条回答
  •  Happy的楠姐
    2020-12-11 04:56

    How timer could know about your shared data?

    Timer callback is executed on some ThreadPool thread. So you will have at least 2 threads:

    1. Your main thread where timer is created and launched;
    2. Thread from ThreadPool for launching callback.

    And it is your responsibility to provide correct work with your shared data.

    Re edits: chibacity provided the perfect example.

提交回复
热议问题