Solved

How to write a timer (gcc C++ on Solaris 10) to repetitively call a function

Posted on 2006-07-07
6
541 Views
Last Modified: 2013-12-05
Have an existing, C++ based system (Solaris 10) which is message-driven (so different functions are called on different events). Whats missing is a time-driven event (say I would like my function Time_Is_Up being called every 100 milliseconds). I assume I need a new thread for that but want to do it in the scope of my main Class to make sure I can utilize other existing functions. How do I do it? (setup the thread, start it etc.)

thanks, JBB
0
Comment
Question by:jerzyb
  • 3
  • 2
6 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 17062395
Just use the POSIX 'alarm()' function which will call the appropriate signal handler using the desired interval, e.g.

#include <sys/types.h>
#include <signal.h>
#include

void timer(int);

int main(int argc,char **argv) {

        signal(SIGALRM, timer);
        alarm(10);
        while(1) pause();
}

void timer() {

  printf("In timer...\n");
  alarm(10); // re-create new alarm
}

Alternatively, you can of course also use threads, see e.g. http://www.llnl.gov/computing/tutorials/pthreads/ ("POSIX Threads Programming")
0
 

Author Comment

by:jerzyb
ID: 17062649
could work, but alarm() does not do milliseconds, and I need at least 100msecs interval.
0
 
LVL 86

Expert Comment

by:jkr
ID: 17062785
Then you can use 'setitimer()' instead of 'alarm()' where the resolution is microseconds, see the manpage at http://www.tiem.utk.edu/cgi-bin/man.cgi?section=2&topic=setitimer

int main(int argc,char **argv) {

    signal(SIGALRM, timer);

    struct itimerval ntimeout;

    ntimeout.it_interval.tv_sec = 0;
    ntimeout.it_interval.tv_usec = 1000; // 1000 ms
    ntimeout.it_value.tv_sec  = seconds;
    ntimeout.it_value.tv_usec = 0;

    setitimer(ITIMER_REAL,&ntimeout);

    while(1) pause();
}

void timer() {

  printf("In timer...\n");
  signal(SIGALRM, timer);
}
0
 

Author Comment

by:jerzyb
ID: 17065886
get some creepy results...looks like timer fires ok but then does not return to the the main proram where it was interrupted; in other words when the main program does nothing (while 1 pause) thats fine, but when the main program does something else, I get unpredictable behavior

JBB
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 17066777
You still can go with http://www.llnl.gov/computing/tutorials/pthreads/ ("POSIX Threads Programming")
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

776 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