We help IT Professionals succeed at work.
Get Started

Please give me a programming example of setitimer ( unix function call) in C++

maha_r asked
Last Modified: 2021-02-26

    I am doing C++ programming in unix. I tried with unix funciton call setitimer. Please find my program snippet attached with this thread. If I remove the static keyword in the program, I'm getting following error.

setitimer.cpp: In member function `void MyClass::registertimer()':
setitimer.cpp:28: error: ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function.  Say `&MyClass::timerevent'
setitimer.cpp:28: error: cannot convert `void (MyClass::*)()' to `void (*)()' in initialization

Please tell me how to sort out this problem. The target of my program is to execute a function at each interval of setitimer.


#include <iostream.h>
#define INTERVAL  2
#include <signal.h>
#include <sys/time.h>
       struct itimerval tout_val;
class MyClass
    public :
       void settimer();
       void registertimer();
       static void timerevent(void);
    private :
void MyClass::settimer()
    tout_val.it_interval.tv_sec = 0;
    tout_val.it_interval.tv_usec = 0;
    tout_val.it_value.tv_sec = INTERVAL; /* set timer for "INTERVAL (10) seconds */
    tout_val.it_value.tv_usec = 0;
    setitimer(ITIMER_REAL, &tout_val,0);
void MyClass::registertimer()
    typedef void(*fp)(void);
    fp ptr = &timerevent;
    signal(SIGALRM,(void(*)(int))ptr); /* set the Alarm signal capture */
void MyClass::timerevent(void)
     cout<<"timer event"<<endl;
     setitimer(ITIMER_REAL, &tout_val,0);
    //setitimer(ITIMER_REAL, &tout_val,0);
int main()
    MyClass myclass;

Open in new window

Watch Question
Principal Engineer
This problem has been solved!
Unlock 2 Answers and 5 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE