A time delayed callback to a class member function

bridged with qdn.public.qnxrtp.applications
Post Reply
Chris Rose

A time delayed callback to a class member function

Post by Chris Rose » Tue Oct 01, 2002 12:10 am

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.

Rennie Allen

Re: A time delayed callback to a class member function

Post by Rennie Allen » Tue Oct 01, 2002 12:10 am

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 <iostream>
using namespace std;

#include <sigc++/signal_system.h>

typedef SigC::Signal0<void> SignalCallback;

static SignalCallback sigcbs[_SIGMAX];

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

static void RegisterSignal(int signum, SigC::Slot0<void> 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();
}
}
-----------------------------------------------------------------

Chris Rose

Re: A time delayed callback to a class member function

Post by Chris Rose » Tue Oct 01, 2002 1:41 am

Where do I get this third party CD?

Rennie Allen

Re: A time delayed callback to a class member function

Post by Rennie Allen » Tue Oct 01, 2002 3:04 am

Chris Rose wrote:
Where do I get this third party CD?
http://www.qnx.com/developer/download/contrib/

Post Reply

Return to “qdn.public.qnxrtp.applications”