You will see that the main application has to sleep for a while (simulating a long time task), and then gets interrupted by a signal that is connected with the timer.
I need the same in Windows platform. Maybe I need a combination of Windows signals with timers...
Somehow I don't understand the problem.
If you call the long time task as a function main will wait for it to finish and then continue with other tasks.
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
#include <time.h>
#define INTERRUPT_TIME 3000000000 //Time(in nanoseconds) after which the main application will be interrupted
#define CLOCKID CLOCK_REALTIME
#define SIG SIGRTMIN
#define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \
} while (0)
staticvoid
handler(int sig, siginfo_t *si, void *uc)
{
(void) si;
(void) uc;
printf("Caught signal %d\n", sig);
signal(sig, SIG_IGN);
}
int
main(int argc, char *argv[])
{
timer_t timerid;
struct sigevent sev;
struct itimerspec its;
longlong freq_nanosecs;
struct sigaction sa;
/* Establish handler for timer signal */
printf("Establishing handler for signal %d\n", SIG);
sa.sa_flags = SA_SIGINFO;
sa.sa_sigaction = handler;
sigemptyset(&sa.sa_mask);
if (sigaction(SIG, &sa, NULL) == -1)
errExit("sigaction");
/* Create the timer */
sev.sigev_notify = SIGEV_SIGNAL;
sev.sigev_signo = SIG;
sev.sigev_value.sival_ptr = &timerid;
if (timer_create(CLOCKID, &sev, &timerid) == -1)
errExit("timer_create");
/* Start the timer */
freq_nanosecs = (INTERRUPT_TIME);
its.it_value.tv_sec = freq_nanosecs / 1000000000;
its.it_value.tv_nsec = freq_nanosecs % 1000000000;
its.it_interval.tv_sec = 0;
its.it_interval.tv_nsec = 0;
if (timer_settime(timerid, 0, &its, NULL) == -1)
errExit("timer_settime");
printf("Sleeping for 50 seconds\n");
sleep(50); //a long running time task
exit(EXIT_SUCCESS);
}
You do not need to read the whole program. The long time task would be 'sleep(50);' which sleeps for 50 seconds. However, after 3 seconds the program gets interrupted and executes the callback 'handler'. If I call 'GetMessage', this call would block my application.
Is there a reason why you can't use SleepEx?
I am not sure if a timer is actually the best solution.
What does this long running task do?
Report progress, error, finished....
You only need to execute this code. The application just reads integers (greater than 1) from stdin and shows them in stdout.
However, if you type 1, a timer is started with a delay of 8 seconds. During those 8 seconds, you cannot input any more values because the condition variable predicate is blocked in 'GetMessage'. So my problem is that the application stops processing notifications for 8 seconds.
My native language is Spanish. I know there are many C++ forums in Spanish, but I like this one. People here are always very kind and helpful (like you). Besides I am trying to improve my English as much as I can ;)
Thank you very much for your answer! The timer callback and the main thread should run in the same thread. I am looking for the same behavior of 'time_create' in Linux. Take a look a this example :
Thanks again for your response. Your proposal is good, but I need a solution without using threads, because that would mean to add synchronization mechanism that could be quite complex.
I have been investigation about timers in Windows, and it seems the application must be in an alertable state to be interrupted by the timer. It seems there is no way in Windows that the timer could interrupt my application in the middle of a long running task.