Solved

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

Posted on 2006-07-07
6
534 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
Comment Utility
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
Comment Utility
could work, but alarm() does not do milliseconds, and I need at least 100msecs interval.
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
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
Comment Utility
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
Comment Utility
You still can go with http://www.llnl.gov/computing/tutorials/pthreads/ ("POSIX Threads Programming")
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
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 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…

762 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

11 Experts available now in Live!

Get 1:1 Help Now