Solved

C++ Simple Posix Timer

Posted on 2010-09-01
4
1,428 Views
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. :)

Thanks!

-w00te
0
Comment
Question by:w00te
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 33577127
You could use 'setitimer()', which would be used like follows (code taken from http://www.cs.uaf.edu/~olawlor/ref/examples/unix/setitimer.c - see also http://www.cs.uaf.edu/~olawlor/ref/examples/unix/setitimer_pc.c)
/* Orion Lawlor's Short UNIX Examples, olawlor@acm.org 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>
#endif

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)) {
		perror("sigaction");
		exit(1);
        }
	
	printStack("main routine");

/* Request SIGPROF */
	itimer.it_interval.tv_sec=0;
	itimer.it_interval.tv_usec=10*1000;
	itimer.it_value.tv_sec=0;
	itimer.it_value.tv_usec=10*1000;
	setitimer(ITIMER_PROF, &itimer, NULL); 
	
/* Just wait a bit, which should get a few SIGPROFs */
	for (i=0;i<1000*1000*10;i++) {
	}

        return(0);
}

Open in new window

0
 
LVL 3

Expert Comment

by:simoesp
ID: 33577722
You can do something like this:

while (true)
{
while (pendingEvents () )
handleEvens ();
sleep (10);
}
0
 
LVL 12

Author Closing Comment

by:w00te
ID: 33578275
Thanks JKR - worked great.
0
 
LVL 12

Author Comment

by:w00te
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. :)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

751 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