Subj : Re: timer thread & signal question To : comp.programming.threads From : VK Date : Sat Jan 08 2005 10:05 pm Loic Domaigne wrote: > You must used queued signal. If your implementation supports it, use > realtime signal: signal whose number is between SIGRTMIN and SIGRTMAX. Thanks Loic, I tried with SIGRTMIN and pthread_sigmask (SIG_BLOCK, &set, NULL); and it works. My next problem now is that when a timer goes off, I want to be able to know which timer id associated with the expired timer. In order to do this, I passed in timer event's sigev_value.sival_ptr the address of timer id. However, when the timer expires, the data stored in siginfo.si_ptr is not the same as the generated timer id eventhough the address is the same. I've been banging my head in the wall for a few hours now. Could you give me some hints of what's gone wrong? Thanks so much again. Here's the new code for my test file. #include #include #include #include #include pthread_t thread; void signalHandler (int signum, siginfo_t* info, void* notused) { pthread_kill(thread, SIGRTMIN ); std::cout << "sending signal...\n"; } void* threadHandler (void*) { siginfo_t info; sigset_t set; struct sigaction sigact; sigemptyset (&sigact.sa_mask); sigact.sa_flags = SA_SIGINFO; sigact.sa_sigaction = signalHandler; sigaction (SIGRTMIN, &sigact, NULL); sigemptyset(&set); sigaddset(&set, SIGRTMIN); while (1) { pthread_sigmask (SIG_BLOCK, &set, NULL); sigwaitinfo(&set, &info); std::cout <<"timerId = " << *(timer_t*)info.si_ptr << "\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 = SIGRTMIN; sig.sigev_value.sival_ptr = (void*)&pTimer; 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 << "created timer id = " << pTimer << "\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's output: [vkincaid@nms2 signal]$ ./a.out created timer id = -1218664448 created timer id = -1218664284 sending signal... timerId = -1219691112 sending signal... timerId = -1219691112 sending signal... sending signal... timerId = -1219691112 timerId = -1219691112 sending signal... timerId = -1219691112 .