I have one problem. I am writing my program on C++ language. I have one problem. I need to set signal handler for my process. As the signal is related with the process on system level I have faced the problem.
My program consists of several classes. They are connected together. But it doesn't matter in this case.
The problem is that I need access to member and methods of the class from my signal handler. For instance, I have a class named Foo at it has some members and methods.
So from my handler I need to call its function and change members.
I understand that compiler should know that this class instances will exist during all program execution.
I have tried to set static member class Foo instance in another class , but this didn't solve the problem.
I have no idea what is the correct approach for doing this. Please explain how to correctly implement signal handling in such case.
Here is an example of my code:
class MyContainer
{
private:
std::vector<Foo> container;
public:
int removeFromContainer(Foo* aFoo) {
// DO some stuff
return RESULT_CODE;
}
int addToContainer(Foo* aFoo) {
// DO some stuff
return RESULT_CODE;
}
};
Here is my Main class
class MainClass
{
private:
int member;
public:
void mainLoop(char* args) {
signal(SIGCHLD, &signalHandler);
}
};
Here is my function for signal handling
void static signalHandler_child(int p)
{
this->myContainerInstance->addToContainer(new Foo);
}
A static method is not so different from a global function. If you need to access instance members of a class, your signal handler should take an instance pointer/reference as argument.
Something like this