I have an object which does some work in an endless loop. The main()
instantiates the object and calls the run()
method. Since I don\'t want to use thr
- What do I need to change to make this work? I think f is like void f(int), like the functions the signal handler gets in some examples.
The compiler complains about the type, therefore you need to pass a function pointer, not an object of type boost::function
. Creating a global variable of this type, and adding a function which calls this object would work :
boost::function myCb;
void CallCb( int value )
{
myCb(value);
}
int main(int argc, char** argv)
{
Foo foo;
struct sigaction sigIntHandler;
myCb = std::bind1st(
std::mem_fun(&Foo::catch_signal), &foo);
f(5); // this call works
sigIntHandler.sa_handler = CallCb;
sigemptyset(&sigIntHandler.sa_mask);
sigIntHandler.sa_flags = 0;
sigaction(SIGTERM, &sigIntHandler, NULL);
s.run();
}
- Do you have any advice how to solve this kind of thing better?
Not really. The idea is ok. I would just just c++11 lambda instead