signal handling

  • Thread starter Lionel van den Berg
  • Start date
L

Lionel van den Berg

Hi all,

I'm trying to do some signal handling using the csignal library but I
can't find and specific examples for C++.

The signal function as defined in C takes a parameter that is the signal
we are registering for, and a second parameter that is the function to call.

What I want to do is specify the function to call as a function of an
instance of "this" class e.g.

void Class::signalHandler( int signal ){}

void Class::someFunction( ) {
signal( SIGINT, this.signalHandler );
}


Obviously this is wrong because it complains about signalHandler not
being a type.

How do I achieve this in C++? Can I achieve this in C++?

Thanks,

Lionel.
 
A

Alf P. Steinbach

* Lionel van den Berg:
I'm trying to do some signal handling using the csignal library but I
can't find and specific examples for C++.

The signal function as defined in C takes a parameter that is the signal
we are registering for, and a second parameter that is the function to call.

What I want to do is specify the function to call as a function of an
instance of "this" class e.g.

void Class::signalHandler( int signal ){}

void Class::someFunction( ) {
signal( SIGINT, this.signalHandler );
}


Obviously this is wrong because it complains about signalHandler not
being a type.

How do I achieve this in C++? Can I achieve this in C++?

You don't. 'signal' is generally incompatible with C++. AFAIK the only
things you can reliably do in a handler are (1) to terminate, (2) to do
nothing, or (3) set a global flag of type 'volatile sig_atomic_t', and the
standard notes in particular that a signal handler using exception handling
is likely to have problems.
 
S

Stephen Howe

Obviously this is wrong because it complains about signalHandler not being
a type.

How do I achieve this in C++?

You can't register member functions as a signal handler. Obviously so,
because "this" points to a specific instance of your class with its
associated class data. When your signal is raised, how does it know what
"this" to supply?

class member functions and function pointers are incompatibile.

What you can register is a static class function, because a static class
function is not tied to any specific instance of a class.
static class functions and function pointers can be compatible.

Stephen Howe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top