Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
LINUX ZIP - UNCOMPRESS WINDOWS PATH 3 87
C++ question 3 62
unix in java example 9 53
looking for unix program to search some keywords in side EAR WAR and JAR files 10 81
Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

910 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

15 Experts available now in Live!

Get 1:1 Help Now