How to access class member and methods from static method(signal handler)

I have one problem. I am writting my program on C++ language. I have one promblem. 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 programm consists several classes. They are connected together. But it doesn't metter in this case.
The problem is that I need to access to member and methods of the class from my signal handler. For instance , I have 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 ideas what is correct approach to do this. Please explain how to correctly implement signal handling in such case.
Thansks.


Here is 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);

}
Last edited on
If you cannot pass any data to the signal handler you need to deal with global variables
You can't do things like new Foo in a signal handler.

When the processing of the abstract machine is interrupted by receipt of a signal, the values of objects which are neither of type volatile std::sig_atomic_t nor lock-free atomic objects are unspecified during the execution of the signal handler, and the value of any object not in either of these two categories that is modified by the handler becomes undefined. - IS


Typically, the signal handler sets a flag (of type volatile std::sig_atomic_t or std::atomic_flag) and returns.
This flag is checked and acted upon during normal processing.
I know that is not correct way to do. But please provide (refactor) my example in correct way. Or provide any other example related to this topic.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <csignal>
#include <thread>
#include <chrono>
#include <functional>

static volatile std::sig_atomic_t flag ;

void set_flag( int signal ) { flag = signal ; }

struct A 
{ 
    int n = 0 ; 
    void mem_fun() { std::cout << ++n << ". A::mem_fun\n" ; /* do whatever */ } 
};

void process_signal( A& object )
{
    while( flag != SIGTERM )
    {
        if( flag == SIGINT )
        {
            flag = 0 ;
            std::cout << "SIGINT " ;
            object.mem_fun() ;
        }
        std::this_thread::sleep_for( std::chrono::milliseconds(100) ) ;
    }
    std::cout << "quitting on SIGTERM\n" ;
}

int main()
{
    A a ;
    std::thread worker( process_signal, std::ref(a) ) ;
    std::signal( SIGINT, set_flag ) ;
    std::signal( SIGTERM, set_flag ) ;

    for( int i = 0 ; i < 5 ; ++i )
    {
        std::raise(SIGINT) ;
        std::this_thread::sleep_for( std::chrono::milliseconds(200) ) ;
    }

    std::raise(SIGTERM) ;
    worker.join() ;
}

http://coliru.stacked-crooked.com/a/21ec6c11fca33a8f
Topic archived. No new replies allowed.