跨平台定时器编码使用函数参考

出于写一个支持跨平台的定时器功能在window平台已经了解用window api 可以实现

CreateTimerQueueTimer(&(HANDLE  htimer), 0, _on_windows_timer, timer, milliSeconds, milliSeconds, WT_EXECUTEINTIMERTHREAD)

具体参数可以自己查window比较好实现,直接设置一下等待出发回调_on_windows_timer(PVOID param, BOOLEAN timer_or_wait_fired)就可以 param 就是对应上面的timer参数,可以自定要需要传递的类型,linux下面找了一些资料可以参考一下这段代码,代码出处

#include 
#include 
#include 
#include 
#include 
#include 
 
//#define CLOCKID CLOCK_REALTIME
#define CLOCKID    CLOCK_MONOTONIC
 
#define SIG SIGRTMIN   //实时信号,可靠信号
 
#define errExit(msg)    do { perror(msg); exit(EXIT_FAILURE); \
        } while (0)
 
 
 
static void
print_siginfo(siginfo_t *si)
{
    timer_t *tidp;
    int or;
 
    tidp = si->si_value.sival_ptr;
 
    printf("    sival_ptr = %p; ", si->si_value.sival_ptr);
    printf("    *sival_ptr = 0x%lx\n", (long) *tidp);
 
    or = timer_getoverrun(*tidp);
    if (or == -1)
        errExit("timer_getoverrun");
    else
        printf("    overrun count = %d\n", or);
}
 
static void handler(int sig, siginfo_t *si, void *uc)
{
    /* Note: calling printf() from a signal handler is not
              strictly correct, since printf() is not async-signal-safe;
              see signal(7)
     */
 
    printf("Caught signal %d\n", sig);
    print_siginfo(si);
    //signal(sig, SIG_IGN);
}
 
 
 
static void handler2(union sigval v)
{
 
    timer_t *tidp;
    tidp=v.sival_ptr;
    printf("Caught handler2 0x%lx\n",*(long *)tidp);//打印传递的参数
 
 
}
 
 
typedef  void (*sigHandler)(int , siginfo_t *, void *);
typedef  void (*pthHandler)(union sigval);
 
//定时器通知方式为信号的处理方式
int testTimerSign(timer_t *pTimerid, struct itimerspec ts, int sig, sigHandler handler){
 
    struct sigevent evp;
    struct sigaction sa;
    int ret=0;
 
    printf("Establishing handler for signal %d\n", SIG);
 
    sa.sa_flags = SA_SIGINFO;
    sa.sa_sigaction = handler;
    sigemptyset(&sa.sa_mask);
 
    //安装信号处理函数
    if (sigaction(sig, &sa, NULL) == -1)
    {
        perror("testTimerSign:sigaction");
        ret=-1;
    }
 
    memset(&evp,0,sizeof(evp));
    evp.sigev_notify =SIGEV_SIGNAL;
    evp.sigev_signo = sig;
    evp.sigev_value.sival_ptr = pTimerid; //传递的参数,指针型
 
    ret = timer_create(CLOCKID, &evp, pTimerid);
    if(ret) {
        perror("timer_create");
    }
 
    printf("timer ID is 0x%lx\n", (long) *pTimerid);
 
    //  ts.it_value.tv_sec = 1;
    //  ts.it_value.tv_nsec = 10000000;
    //  ts.it_interval.tv_sec = its.it_value.tv_sec;
    //  ts.it_interval.tv_nsec = its.it_value.tv_nsec;
    printf("testTimerSign:start timer\n");
    ret = timer_settime(*pTimerid, 0, &ts, NULL);
    if(ret) {
        perror("timer_settime\n");
    }
    return ret;
}
 
//定时器通知方式为线程方式
int testTimerThread(timer_t *pTimerid,struct itimerspec its,pthHandler handler)
{
    struct sigevent sev;
    int ret=0;
 
    memset(&sev,0,sizeof(sev));
    sev.sigev_notify = SIGEV_THREAD;
    sev.sigev_notify_function=handler;//处理函数
    sev.sigev_value.sival_ptr = pTimerid;//传递给处理函数的参数,指针型
    //sev.sigev_value.sival_int=timerid;
    if (timer_create(CLOCKID, &sev, pTimerid) == -1)
    {
        perror("timer_create\n");
        ret=-1;
    }
 
    printf("timer ID is 0x%lx\n", (long) *pTimerid);
 
 
    //  its.it_value.tv_sec = 1;
    //  its.it_value.tv_nsec = 10000000;
    //  its.it_interval.tv_sec = its.it_value.tv_sec;
    //  its.it_interval.tv_nsec = its.it_value.tv_nsec;
 
    if (timer_settime(*pTimerid, 0, &its, NULL) == -1)
    {
        perror("timer_settime\n");
        ret=-1;
    }
 
    return ret;
 
}
 
int
main(int argc, char *argv[])
{
    timer_t timerid1,timerid2;
    struct  itimerspec ts1,ts2;
 
    if (argc != 2) {
        fprintf(stderr, "Usage: %s \n",
                argv[0]);
        exit(EXIT_FAILURE);
    }
 
    ts1.it_value.tv_sec =  1;  //定时器初次触发时间
    ts1.it_value.tv_nsec = 0;
    ts1.it_interval.tv_sec = ts1.it_value.tv_sec;  //定时器周期性间隔时间
    ts1.it_interval.tv_nsec = ts1.it_value.tv_nsec;
 
    ts2.it_value.tv_sec =  4;  //定时器初次触发时间
    ts2.it_value.tv_nsec = 0;
    ts2.it_interval.tv_sec = 2;  //定时器周期性间隔时间
    ts2.it_interval.tv_nsec = 0;
 
    testTimerSign(&timerid1,ts1,SIG,handler);
    testTimerThread(&timerid2,ts2,handler2);
 
    /*
     *
     * Sleep for a while; meanwhile, the timer may expire
              multiple times
     */
 
    printf("Sleeping for %d seconds\n", atoi(argv[1]));
    while(1)
        sleep(atoi(argv[1]));
 
 
    exit(EXIT_SUCCESS);
}
 
 

主要不理解的是怎么来像window那样可以自定义传递参数,查过继续查询资料参数参考一下这边文档得出结论,其实就是通过设置struct sigevent evp这个结构体的evp.sigev_value.sival_ptr这个变量来传递指针型参数,。

你可能感兴趣的:(游戏编程)