gpt4 book ai didi

c++ - 使用 timer_create 传递用户数据

转载 作者:太空狗 更新时间:2023-10-29 20:47:20 29 4
gpt4 key购买 nike

我正在使用 timer_create 在 Linux 中创建一个计时器。回调原型(prototype)为:

static void TimerHandlerCB(int sig, siginfo_t *extra, void *cruft)

我如何传递用户数据,以便我可以在计时器到期后调用的回调中接收相同的数据。

这是我的示例代码:

int RegisterTimer(iDiffInTime)
{
struct sigaction sa;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_SIGINFO; /*call our handler*/
sa.sa_sigaction = TimerHandlerCB;/*Event handler to be called after timer expires*/
if(sigaction(SIGRTMAX, &sa, NULL) < 0)
{
perror("sigaction");
return 1;
}
// Setup the timer
sigevent sigev;
timer_t timerid = 0;

memset(&sigev, 0, sizeof(sigev));

sigev.sigev_notify = SIGEV_SIGNAL;
sigev.sigev_signo = SIGRTMAX;
sigev.sigev_value.sival_ptr = &timerid;

timer_t timerid;

if (timer_create(CLOCK_REALTIME, &sigev, &timerid) == 0)
{
printf("Timer created\n");
struct itimerspec itval, oitval;

itval.it_value.tv_sec = iDiffInTime * 1000;
itval.it_value.tv_nsec = 0;
itval.it_interval.tv_sec = 0;
itval.it_interval.tv_nsec = 0;

//Populate handles required in TimerCallback
Display_Handle hDisplay = ......//
Buffer_Handle hBuf = .....//
if (timer_settime(timerid, 0, &itval, &oitval) != 0)
{
perror("time_settime error!");
return 1;
}
}
else
{
perror("timer_create error!");
}
return 0
}

我在哪里传递 hDisplayhBuf 以便我可以在 TimerHandlerCB 中接收它们?

最佳答案

你已经在做了:

sigev.sigev_value.sival_ptr = &timerid;

您可以在其中指定任何内容(指定 &timerid 通常用于区分多个计时器,但不是必需的)。现在在你的处理程序中:

static void timer_handler(int signo, siginfo_t *si, void *uc)
{
/* si->si_value.sival_ptr */
}

引自 TLPI

When calling timer_create(), evp.sigev_value.sival_ptr is typically assigned the address of the timerid argument given in the same call. Alternatively, evp.sigev_value.sival_ptr may be assigned the address of a structure that contains the timerid given to timer_create()

关于c++ - 使用 timer_create 传递用户数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5947395/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com