c++ Implementing Timed Callback function

前端 未结 4 644
别跟我提以往
别跟我提以往 2020-12-04 22:18

I want to implement some system in c++ so that I can call a function and ask for another function to be called in X milliseconds. Something like this:

callfu         


        
4条回答
  •  星月不相逢
    2020-12-04 23:00

    Do you want it to by asynchronous so that the callback gets executed when 25 miliseconds are over without blocking the main executing thread? If so, you can execute the callback in a separate thread from the timer / timed callback function you implement.

    If you do not use multithreading, then your main or the calling function of callfunctiontimed(25, funcName); would block while you run a sleep / usleep. It is your choice now as to what behavior you want to implement.

    The real solution would not be as simple as to multithread or not. There are things like, how do you keep different timers/callbacks information considering the function can be called multiple times with different timeouts and functions.

    One way to do it, would be like this:

    1. Create a sorted list of timers/callbacks, sorted based on the time to expire.
    2. Have on main thread and one thread which looks at callbacks/timers, call it timer thread.
    3. When a new callback is added, add it in the sorted list.
    4. The timer thread could be initialized to wait for the least amount of time in the sorted list, or the head. Re-initialize it as new callbacks get added. There would be some math and conditions to take care of.
    5. As the timer thread is done sleeping, it removes and looks at head of the list and executes the function pointer in a new thread. Timer thread is re-initialized with sleep time on the new head of the list.

      main() {
              //spawn a timer thread with pthread create 
      
          callfunctiontimed(25, test); 
          callfunctiontimed(35, show);
          callfunctiontimed(4,  print);
      }
      
      callfunctionTImed(int time, (func*)function, void*data) //
      {
          //add information to sorted list of timer and callbacks
          //re-initialize sleep_time for timer thread if needed. 
          return. 
      }
      timerThread() {
          while(1){
           sleep(sleep_time);
           //look at head of timer list, remove it, call function in a new thread   
           //adjust sleep time as per new head     
          }
      }
      

    Hopefully this gives an idea of what I meant, although this is not perfect and has several problems.

提交回复
热议问题