I'm trying to create a signal handler thread to handle SIGALRM signals.
The main thread calls the following function before creating any other threads
As you can see, I block the alarm signal.Code:pthread_t Timer::Init(long _clock_interval_musec) { // Set interval. clock_interval = _clock_interval_musec; // This is called while in a single threaded environment. // Main thread masks alarm. sigset_t alarm_off_mask; if ((sigemptyset(&alarm_off_mask) == -1) || (sigaddset(&alarm_off_mask, SIGALRM) == -1)) perror("Failed to initialize the signal set"); if (pthread_sigmask(SIG_BLOCK, &alarm_off_mask, NULL) == -1) perror("Failed to block SIGALRM"); // Now call a new thread which handles alarm signals. pthread_t retID; pthread_create(&retID, NULL, TimerHandlerThread, NULL); // Return id of thread responsible for managing alarm signals. return retID; }
The spawned signal handler thread has the following Run function
Sigwait never returns ("we are here" never prints).Code:void* Timer::TimerHandlerThread(void* args) { // Change granualirity of alarm. itimerval tv; tv.it_interval.tv_sec = clock_interval / USECS_IN_SECS; tv.it_interval.tv_usec = clock_interval % USECS_IN_SECS; setitimer(ITIMER_REAL, &tv, NULL); // Unmask alarm signals in this thread. sigset_t sigset; if ((sigemptyset(&sigset) == -1) || (sigaddset(&sigset, SIGALRM) == -1)) perror("Failed to add signal to wait for"); Timer currentTime; int sig; while (1) { /* wait for a signal to arrive */ sigwait(&sigset, &sig); std::cout << "we are here\n"; if (sig == SIGALRM && g_timerQueue.size() > 0) { currentTime.Set(0); // Handle if needed g_mutex.P(); if (*(g_timerQueue.front()) < currentTime) { g_timerQueue.front()->Event_Handler(); std::pop_heap(g_timerQueue.begin(), g_timerQueue.end()); g_timerQueue.pop_back(); } g_mutex.V(); } } return NULL; }
clock_interval is 50.
Is the timer not firing?
Did I not set my interval correctly?
Did I not proper block the signals ensuring the that the default handler would not take care of them?
Any help is welcome.![]()


Reply With Quote

Bookmarks