L
lpw
I have dilligently reviewed FAQ-lite Section 3.2, "How do I pass a
pointer-to-member-function to a signal handler, X event callback, system
call that starts a thread/task, etc." The only suggestion on how to deliver
a signal to an object is to do it via a global variable and a wrapper
function, a technique that is generally a Bad Idea (due to the usage of a
global variable). I understand that this ng is dedicated to the discussion
of platform-independent C++ issues, and that signals are primarily an
artifact of UNIX. My question is, however, more related to the proper way
of structuring my C++ code rather than the specifics of signal handling.
I've been racking my brain on how to incorporate signal handling into my C++
programs whilst remaining one with the OO nature. I would like to propose a
possible solution, one that, IMHO, is a little cleaner and fits better with
the OO paradigm than simply using global object pointers.
Suppose that we have a program that does a lot of I/O (sockets, pipes,
files, etc.). For each I/O "stream", we have a dedicated handler object.
When the user becomes bored and sends SIGINT to our program, we would like
all our handlers to cleanly close their respective TCP connections, IPC
pipes, etc. With things like connection-oriented sockets, this is often
more involved than merely calling close(). The responsibility to cleanly
shut down a particular I/O "stream" should be delegated to that stream's
handler object. Thus, we need object-level signal delivery to a number of
heterogeneous objects. This can be accomplished by having all those objects
derive from a common base class. The responsibility of that class (let's
call it Interruptible) is to maintain a list of all live Interruptible
objects and invoke the signal handlers of those objects whenever SIGINT is
caught. The code below illustrates this paradigm using a silly Runner class
instead of an I/O handler. But the idea remains the same.
#include <iostream>
#include <string>
#include <list>
#include <cstdlib>
#include <ctime>
#include <signal.h>
#include <unistd.h>
using namespace std;
class Interruptible {
// "global" list of live interruptible objects
static list<Interruptible*> instances;
protected:
// all interruptible classes must implement this method
virtual void sigint_handler() = 0;
Interruptible() {
// add me to global list of live interruptible objects
instances.push_back(this);
}
virtual ~Interruptible() {
// remove me from global list of live interruptible objects
instances.remove(this);
}
public:
// the "global" signal handler
static void sigint_handler(int s) {
// call all interruptible objects
list<Interruptible*>::const_iterator i;
for (i=instances.begin(); i!=instances.end(); i++)
(*i)->sigint_handler();
// goodbye
exit(0);
}
};
list<Interruptible*> Interruptible::instances;
class Runner : public Interruptible {
string name;
virtual void sigint_handler() {
cout << name << " got SIGINT " << endl;
//
// do some cleanup here
//
}
public:
Runner(const string& str) : Interruptible(), name(str) { }
void run() {
cout << name << " is running" << endl;
//
// do something smart and useful here
//
sleep(1+rand()&3);
}
};
int main(void) {
signal(SIGINT, Interruptible::sigint_handler);
srand(time(NULL));
Runner obj1("Runner 1");
Runner obj2("Runner 2");
while (true) {
obj1.run();
obj2.run();
}
}
I am most keen on receiving your comments, questions, flames, death threats,
and/or suggestions on the proposed solution. Again, I am more interested in
writing good object-oriented C++ code rather than the platform specific
mechanisms of signal delivery. I would love to hear of other possible
approaches. Just please don't say that this is completely off topic.
pointer-to-member-function to a signal handler, X event callback, system
call that starts a thread/task, etc." The only suggestion on how to deliver
a signal to an object is to do it via a global variable and a wrapper
function, a technique that is generally a Bad Idea (due to the usage of a
global variable). I understand that this ng is dedicated to the discussion
of platform-independent C++ issues, and that signals are primarily an
artifact of UNIX. My question is, however, more related to the proper way
of structuring my C++ code rather than the specifics of signal handling.
I've been racking my brain on how to incorporate signal handling into my C++
programs whilst remaining one with the OO nature. I would like to propose a
possible solution, one that, IMHO, is a little cleaner and fits better with
the OO paradigm than simply using global object pointers.
Suppose that we have a program that does a lot of I/O (sockets, pipes,
files, etc.). For each I/O "stream", we have a dedicated handler object.
When the user becomes bored and sends SIGINT to our program, we would like
all our handlers to cleanly close their respective TCP connections, IPC
pipes, etc. With things like connection-oriented sockets, this is often
more involved than merely calling close(). The responsibility to cleanly
shut down a particular I/O "stream" should be delegated to that stream's
handler object. Thus, we need object-level signal delivery to a number of
heterogeneous objects. This can be accomplished by having all those objects
derive from a common base class. The responsibility of that class (let's
call it Interruptible) is to maintain a list of all live Interruptible
objects and invoke the signal handlers of those objects whenever SIGINT is
caught. The code below illustrates this paradigm using a silly Runner class
instead of an I/O handler. But the idea remains the same.
#include <iostream>
#include <string>
#include <list>
#include <cstdlib>
#include <ctime>
#include <signal.h>
#include <unistd.h>
using namespace std;
class Interruptible {
// "global" list of live interruptible objects
static list<Interruptible*> instances;
protected:
// all interruptible classes must implement this method
virtual void sigint_handler() = 0;
Interruptible() {
// add me to global list of live interruptible objects
instances.push_back(this);
}
virtual ~Interruptible() {
// remove me from global list of live interruptible objects
instances.remove(this);
}
public:
// the "global" signal handler
static void sigint_handler(int s) {
// call all interruptible objects
list<Interruptible*>::const_iterator i;
for (i=instances.begin(); i!=instances.end(); i++)
(*i)->sigint_handler();
// goodbye
exit(0);
}
};
list<Interruptible*> Interruptible::instances;
class Runner : public Interruptible {
string name;
virtual void sigint_handler() {
cout << name << " got SIGINT " << endl;
//
// do some cleanup here
//
}
public:
Runner(const string& str) : Interruptible(), name(str) { }
void run() {
cout << name << " is running" << endl;
//
// do something smart and useful here
//
sleep(1+rand()&3);
}
};
int main(void) {
signal(SIGINT, Interruptible::sigint_handler);
srand(time(NULL));
Runner obj1("Runner 1");
Runner obj2("Runner 2");
while (true) {
obj1.run();
obj2.run();
}
}
I am most keen on receiving your comments, questions, flames, death threats,
and/or suggestions on the proposed solution. Again, I am more interested in
writing good object-oriented C++ code rather than the platform specific
mechanisms of signal delivery. I would love to hear of other possible
approaches. Just please don't say that this is completely off topic.