Link to home
Start Free TrialLog in
Avatar of maha_r
maha_r

asked on

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

Hi,

    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.

Thanks,
Maha

#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;
    myclass.settimer();
    myclass.registertimer();
    while(1)
    {
    }
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Christopher Fenton
Christopher Fenton
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
With all due respect to FentonEng, I think that his suggestion should not be prescribed as the best practice. I do not care about the points and I dont't think that the author of so small a question abandoned so long ago is interested in the ultimate truth, but if the question ever gets into the search engines, it does not look responsible to preach "just don't mess with C++, use plain C".
I think that delete is a fair action: for example, https://www.experts-exchange.com/questions/23373240/illegal-call-of-non-static-member-function.html gives an answer to a similar question.