473,287 Members | 1,650 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

Object-level signal delivery

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.
Nov 1 '05 #1
2 2175
"lpw" <lw*********@hotmail.com> wrote in message
news:Fo********************@rogers.com...
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.


Your problem is going to deal with you not having any form of locking.
Consider your statement:
for (i=instances.begin(); i!=instances.end(); i++)
(*i)->sigint_handler();

What happens when this is running in one thread, and some other thread is
pushing opjects onto the list? I.E. Somewhere you've called sigint_handler
while at the same time some thread is creating a derived of Interruptible.

In your current program this doesn't seem to be a problem as you don't have
any threads creating other threads, but other cases you may want to do that.
Nov 1 '05 #2

lpw wrote:
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."
I have done something similar. We use it as architecture for our
Multitasking projects. Basically it boils down to passing callback
wrappers over an associated queue (or server). All the concurrent (or
psuedo concurrent) threads/task consist of a handler that blocks on a
queue (also known as mailbox) expecting callback wrapper bases only
(use encapsulating teqniques that prevent any thing else from being
sent over mailbox). The callback is then executed on receipt, upon
whereafter the wrapper is destroyed and the queue is monitored again.
It gets interesting - wrappers that handle member functions with
arguments, what if class instances don't exist when the delayed member
function call is made, etc.

It is a kind of nice pattern though, because it allows for marrying OOP
with multitasking, almost abstracting the multitasking from the OOP
specialist.
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).
Obviously a global variable is not needed to deliver a wrapper to a
"Worker Thread". Encapsulation can be used - the thread is then
wrapped. Most threads allow arguments on their entry points - an
argument can be the pointer to an object associated with that
particular object. The entry point to the tread itself being a static
member function, therefore having access to instance internals (and
it's associated mailbox).

Point is global variables are not the only way.
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 don't know about signals, but you can certainly wrap member
functions, and access them in the context associated with a particular
object - if you want to only associate an object with one context.
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.
Who ever used global object pointers?
I think I have done something that

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.
I don't think a sigint is required to do this. Using queueing
mechanisms, finishing the current job at hand and then fetching the
next request from a queue should work? IMHO that's the more OO
approach. I've used these types of approaches for comms (TCP/IP).
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.
In who's context does the interruptible object normally execute. In
which context does the sigint handler execute. How does these contexts
influence each other? Sigint handler seems to be in an orthogonal
context in your example.
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.


We use the command pattern for this. We pass commands (encapsulating
member function pointers and their receivers) between threads using
queues. I have not seen this technique before :-). But maybe I have not
looked enough. A lot of work because you need to build the architecture
- not OS specific, using the bridge pattern, mostly. Has some problems
of itself, but I think they can be overcome - especially now since
boost has arrived. Is this what you had in mind? Communication between
threads in an OO way?

Regards,

W

Nov 3 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: Daniel | last post by:
Hello =) I have an object which contains a method that should execute every x ms. I can use setInterval inside the object construct like this - self.setInterval('ObjectName.methodName()',...
9
by: Keith Rowe | last post by:
Hello, I am trying to reference a Shockwave Flash Object on a vb code behind page in an ASP.NET project and I receive the following error: Guid should contain 32 digits with 4 dashes...
11
by: DrUg13 | last post by:
In java, this seems so easy. You need a new object Object test = new Object() gives me exactly what I want. could someone please help me understand the different ways to do the same thing in...
44
by: Steven T. Hatton | last post by:
This may seem like such a simple question, I should be embarrassed to ask it. The FAQ says an object is "A region of storage with associated semantics." OK, what exactly is meant by "associated...
16
by: sneill | last post by:
How is it possible to take the value of a variable (in this case, MODE_CREATE, MODE_UPDATE, etc) and use that as an object property name? In the following example I want 'oIcon' object to have...
9
by: Andrew Au | last post by:
Dear all, I am trying to write a piece of software that use Object Oriented design and implement it with C, I did the following == In Object.h == typedef struct ObjectStructure* Object; ...
5
by: Matthew | last post by:
I have a nice little Sub that saves data in a class "mySettings" to an XML file. I call it like so: Dim mySettings As mySettings = New mySettings mySettings.value1 = "someText" mySettings.value2...
4
by: Luke Matuszewski | last post by:
Here are some questions that i am interested about and wanted to here an explanation/discussion: 1. (general) Is the objectness in JavaScript was supported from the very first version of it (in...
3
by: User1014 | last post by:
A global variable is really just a property of the "Global Object", so what does that make a function defined in the global context? A method of the Global Object? ...
2
by: Ralph | last post by:
Hi I don't understand why it's not working: function schedule(imTop){ this.tdImagesTop = imTop; } schedule.prototype.selectEl = function() { alert(this.tdImagesTop);
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.