A time delayed callback to a class member function

I need to execute a private method in my class after a set time delay from
executing one of the public methods. It is basically a class that that
controls a device. The device can be turned on/off via the public interface
but when the device is turned on it will turn itself off again after a
specified time. What is a recommended way to do this?
I tried using: “signal(SIGALRM, DeviceOff)” where DeviceOff is a method of
my class declared as follows:
void MyClass::DeviceOff(int sig);

This seems to not work because the callback function for the signal is a
method in a class and not a stand alone function.

Chris Rose wrote:

I need to execute a private method in my class after a set time delay from
executing one of the public methods. It is basically a class that that
controls a device. The device can be turned on/off via the public interface
but when the device is turned on it will turn itself off again after a
specified time. What is a recommended way to do this?
I tried using: “signal(SIGALRM, DeviceOff)” where DeviceOff is a method of
my class declared as follows:
void MyClass::DeviceOff(int sig);

This seems to not work because the callback function for the signal is a
method in a class and not a stand alone function.

Here is a way of doing this using signals/slots (requires that you
install libsigc++ from the 3rd party CD). Instances of differing
classes can attach member functions to any signal (in this case
incrementCount is public since it is accessed from client code, but if
RegisterSignal were called - for instance - inside the constructor of
foo, incrementCount could certainly be private).

I just typed this off the top of my head, so you might want to consider
having sigcbs be an array of pointers to the head of a list of
SignalCallbacks (this code is purely illustrative, although it does
compile and run). Once compiled, run with no arguments, and hit Ctrl-C
to see it work, you can kill it off with a -9.

You could also roll your own functors as well.

-----------------------------------------------------------testsig.cc----------
#include <signal.h>
#include <unistd.h>
#include
using namespace std;

#include <sigc++/signal_system.h>

typedef SigC::Signal0 SignalCallback;

static SignalCallback sigcbs[_SIGMAX];

static void sigredir(int signum)
{
sigcbs[signum].emit();
}

static void RegisterSignal(int signum, SigC::Slot0 sig)
{
sigcbs[signum].connect(sig);
signal(signum, sigredir);
}

class foo: public SigC::Object
{
private:
int cnt;
public:
foo(int count=0):cnt(count) {}

void incrementCount(void)
{
++cnt;
}

void showCount(void)
{
cout << cnt << ‘\n’;
}
};

main()
{
foo bar;

RegisterSignal(SIGINT, slot(bar, &foo::incrementCount));

while(1) {
sleep(5);
bar.showCount();
}
}

Where do I get this third party CD?

Chris Rose wrote:

Where do I get this third party CD?

http://www.qnx.com/developer/download/contrib/