Subj : Posix timer thread and signal segfault To : comp.programming.threads From : VK Date : Tue Jan 18 2005 03:37 pm Hi, I hope I'm posting to the right group. If not, could you point me to a correct group. Thanks a lot. I have a timer thread that sends timeout signal to the signal handler when a timer expires. Then the signal handler sends SIGRTMIN to the thread handler. When creating a timer, I assign the address of the generated timer id to the sigevent's info. The siginfo returned in signalHandler seems to always contain valid info. However, the siginfo returned in threadHandler from calling sigwaitinfo() *sometimes* gives me an invalid address of the timer id. The result is of course a segfault. I have no idea why this happens. Any help is really appreciated. I've been looking at this code for a day now and still can't figure it out. Thanks so much. Below is my test code. #include #include #include #include #include pthread_t thread; timer_t* pTimer; int expired = 0; void signalHandler (int signum, siginfo_t* info, void* notused) { pthread_kill(thread, SIGRTMIN ); std::cout << "sigHandler tid = " << *(timer_t*)(info->si_ptr); std::cout << " @ " << (timer_t*)(info->si_ptr) << std::endl; } timer_t timerStart (int sec, int nsec, int sec_itv, int nsec_itv) { struct sigevent sig; struct itimerspec req, old; sig.sigev_notify = SIGEV_SIGNAL; sig.sigev_signo = SIGRTMIN; pTimer = new timer_t; sig.sigev_value.sival_ptr = (void*)pTimer; req.it_value.tv_sec = sec; req.it_value.tv_nsec = nsec; req.it_interval.tv_sec = sec_itv; req.it_interval.tv_nsec = nsec_itv; timer_create (CLOCK_REALTIME, &sig, pTimer); timer_settime(*pTimer, 0, &req, &old); std::cout << "FINAL timer id " << *pTimer; std::cout << " @ " << pTimer << "\n"; return *pTimer; } 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); memset (&info, 0, sizeof(siginfo_t)); sigwaitinfo(&set, &info); std::cout << "timerId @ " << (timer_t*)info.si_ptr << std::endl; std::cout <<" timerId " << *(timer_t*)info.si_ptr << "\n"; timer_delete(*(timer_t*)info.si_ptr); delete ((timer_t*)info.si_ptr); expired = 1; } pthread_exit(NULL); } static void intr_sleep (int nsec) { struct timespec ts; ts.tv_sec = 0; ts.tv_nsec = nsec; while (nanosleep (&ts, &ts) == -1 && errno == EINTR) ; } int main() { pthread_attr_t attr; pthread_attr_init(&attr); pthread_create(&thread, &attr, threadHandler, NULL); int count = 0; while (1) { expired = 0; timer_t t0 = timerStart(0,25000,0,0); std::cout << "count = " << count++ << "\n"; while (!expired) intr_sleep(1000); } pthread_join(thread, NULL); return 0; } .