Using c++ function pointer as signal handler. -
i'd add new handler signal sigusr1 in code. here's signal signature form header file signal.h :
void (*signal(int, void (*)(int)))(int);
my handler member function, i'm using std::bind make function fit in signal accepted input.
myclass::my_handler(int x); here's conversion of member function signal accepted input:
std::bind(&myclass::my_handler, this, std::placeholders::_1); however, std::bind return c++ representation of function pointer (a.k.a std::function<void(int)>) , need c representation (void)(*)(int).
should casting forcefully, or perhaps there's c++ alternative signal ?
thanks
there no portable way convert c++ function c function because can have different abis.
what can declare global variable cpphandler -
std::function<void(int)> cpphandler = null; also declare function -
extern "c" { void signal_handler(int i); } void signal_handler(int i){ cpphandler(i); return; } now in function want create binding -
cpphandler = std::bind(&myclass::my_handler, this, std::placeholders::_1); signal(x, signal_handler); //replace x whatever signal want install this makes sure function signal_handler created c abi. , calls c++ function code c++ abi.
now can use signal function signal_handler.
Comments
Post a Comment