Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2006-07-07
6
Medium Priority
?
559 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
[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
  • 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 1500 total points
ID: 17066777
You still can go with http://www.llnl.gov/computing/tutorials/pthreads/ ("POSIX Threads Programming")
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 …
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.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

704 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