Solved

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

Posted on 2006-07-07
6
543 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

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

Suggested Solutions

Title # Comments Views Activity
Oracle Finace 3 88
How to Insert a File Using Text Editor 9 93
Embarcadero C++ builder XE10.1 Berlin TRegistry declaration 1 41
learn programming 8 42
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
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.

828 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