C++ Simple Posix Timer

Posted on 2010-09-01
Last Modified: 2013-11-17
Hey guys,

What's the simplest bare bones method of creating a timer to handle this situation:

I have a program running that will always be getting messages and doing internal processing.  Every 10 seconds on top of this (or some other arbitrary number), I would like to get a signal from a timer which calls one of the methods on a singleton class within the program (just a callback really).

Anyway, I'm aware of the thread synch issues and all, but it's been a while since I've made a timer in a Unix/Linux environment so some code would be useful. :)


Question by:w00te
  • 2
LVL 86

Accepted Solution

jkr earned 500 total points
ID: 33577127
You could use 'setitimer()', which would be used like follows (code taken from - see also
/* Orion Lawlor's Short UNIX Examples, 2004/3/5

Shows how to use setitimer to get periodic interrupts.


#include <stdio.h>

#include <stdlib.h>

#include <signal.h>

#include <sys/time.h>

#ifdef SOLARIS /* needed with at least Solaris 8 */

#include <siginfo.h>


void printStack(const char *where) {

	printf("%s stack: %p\n",where,&where);


void signalHandler(int cause, siginfo_t *HowCome, void *ucontext) {

	printStack("signal handler");


int main(){ 

	int i;

	struct itimerval itimer;


/* Install our SIGPROF signal handler */

        struct sigaction sa;


        sa.sa_sigaction = signalHandler;

        sigemptyset( &sa.sa_mask );

        sa.sa_flags = SA_SIGINFO; /* we want a siginfo_t */

        if (sigaction (SIGPROF, &sa, 0)) {





	printStack("main routine");

/* Request SIGPROF */





	setitimer(ITIMER_PROF, &itimer, NULL); 


/* Just wait a bit, which should get a few SIGPROFs */

	for (i=0;i<1000*1000*10;i++) {




Open in new window


Expert Comment

ID: 33577722
You can do something like this:

while (true)
while (pendingEvents () )
handleEvens ();
sleep (10);
LVL 12

Author Closing Comment

ID: 33578275
Thanks JKR - worked great.
LVL 12

Author Comment

ID: 33578280
Oh and thanks for the other answer too, I just didn't want to touch the sleep function if I didnt have to. :)

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Embarcadero C++ Builder XE2 TDateTime 8 61
How to debug Angular JS in Visual Studio 2013 2 108
Dialogbox API leak? 18 81
Memory going from 12gb to 64gb or 96gb. worth it? 15 135
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now