Subj : timer thread & signal question To : comp.programming.threads From : VK Date : Thu Jan 06 2005 08:07 pm Hi, Please help me with this newbie problem. I'm trying to set up a timer thread with the signal handler. My problem is that when I have 2 timers that go off at the same time, the signal SIGUSR2 is sent twice to sigwait(), but since they are 2 identical signals, sigwait() sees it as 1 signal only. How do I tell sigwait() that there are 2 of the same signal? I tried sigwaitinfo() also as I thought sigwaitinfo() "returns the queued signal value so that applications can differentiate between multiple events queued to the same signal number". But it didn't do it :( Can anyone please help me? Thanks a lot in advance. My test file is as below. #include #include #include #include #include pthread_t thread; void signalHandler (int signum) { pthread_kill(thread, SIGUSR2 ); std::cout << "sending signal...\n"; } void* threadHandler (void*) { int signum; sigset_t set; struct sigaction sigact; sigact.sa_handler = signalHandler; sigaction (SIGUSR2, &sigact, NULL); sigemptyset(&set); sigaddset(&set, SIGUSR2); while (1) { sigwait(&set, &signum); std::cout <<"Signal received!\n"; } pthread_exit(NULL); } timer_t timerStart (int sec, int sec_itv) { timer_t pTimer; struct sigevent sig; struct itimerspec req, old; sig.sigev_notify = SIGEV_SIGNAL; sig.sigev_signo = SIGUSR2; req.it_value.tv_sec = sec; req.it_value.tv_nsec = 0; req.it_interval.tv_sec = sec_itv; req.it_interval.tv_nsec = 0; if (timer_create (CLOCK_REALTIME, &sig, &pTimer)) std::cout <<"timer create failed\n"; if (timer_settime(pTimer, 0, &req, &old)) std::cout <<"timer settime failed\n"; std::cout << "timer is created and set\n"; return pTimer; } int main() { pthread_attr_t attr; pthread_attr_init(&attr); pthread_create(&thread, &attr, threadHandler, NULL); timer_t t0 = timerStart(4,0); timer_t t1 = timerStart(2,1); pthread_join(thread, NULL); return 0; } Program output: [vkincaid@nms2 signal]$ g++ -lrt -lpthread TimerThread.cpp [vkincaid@nms2 signal]$ ./a.out timer is created and set timer is created and set --> wait for 2 secs sending signal... --> t1 fires Signal received! sending signal... --> t1 fires Signal received! sending signal... --> t0 fires sending signal... --> t1 fires at the same time Signal received! --> signal is only received once sending signal... Signal received! Ctrl-C .