linux 下可以使用 timerfd_create 一系列函数
win32下 CreateThreadpoolTimer 或者 CreateTimerQueueTimer 都可以用进程默认的线程池来使用回调
对于 DeleteTimerQueueTimer 以及 DeleteTimerQueueEx 最后一个参数需要注意一下
如果是 INVALID_HANDLE_VALUE : callback如果正在调用则等待回调完成
如果是 NULL : callback 如果正在调用,则GetLastError 为ERROR_IO_PENDING,立即返回
只做最简单的封装
第一个例子 CreateThreadpoolTimer , 利用进程默认线程池回调
#include <Windows.h>
#include <iostream>
#include <functional>
class TimeQueue
{
static const int __iUnitPerSeconds; //SetThreadpoolTimer 以 100ns 为单位
public:
PTP_TIMER addTimer(const long long & iSeconds , const DWORD & interval , void * cb = NULL) {
// 相对时间 iSeconds 需要一个负数
PTP_TIMER timer = CreateThreadpoolTimer(&TimeQueue::callback,cb,0);
if(NULL == timer){
perror("创建定时器失败");
return NULL;
}
ULARGE_INTEGER li;