timer class in linux

后端 未结 5 1581
清歌不尽
清歌不尽 2021-02-04 22:27

I need a timer to execute callbacks with relatively low resolution. What\'s the best way to implement such C++ timer class in Linux? Are there any libraries I could use?

5条回答
  •  有刺的猬
    2021-02-04 23:18

    Try the clock_gettime() function, defined in time.h:

    int clock_gettime(clockid_t clk_id, struct timespec *tp);
    
    struct timespec {
      time_t   tv_sec;        /* seconds */
      long     tv_nsec;       /* nanoseconds */
    };
    

    Typically you might call it like this:

    struct timespec ts;
    clock_gettime(CLOCK_REALTIME, &ts);
    

提交回复
热议问题