Overview

Deferred Callback

This small library implements a trivial deferred callback.

For examples:

The library

::

typedef DeferredCallback<boost::function<void(int)> > Prod; typedef boost::shared_ptr<Prod> ProdPtr;

void blockingMultilpy(ProdPtr prod, int a, int b) {
int c = a * b; prod->setSuccess(c);

}

ProdPtr asyncMultiply(int a, int b) {
ProdPtr prod(new Prod); // spawn another thread to multiply a and b, // when done, notify prod boost::thread(blockingMultiply, prod, a, b); return prod;

}

The consumer

::
void displayResult(int c) {
std::cout << "Result is " << c << std::endl;

}

void invoke(int a, int b) {
ProdPtr prod = asyncMultiply(a, b); prod->addCallback(boost::bind(displayResult, _1));

}

int main() {
invoke(7, 13); // go on and do something else while the multiplication // is carried out boost::posix_time::seconds workTime(1); boost::this_thread::sleep(workTime); return 0;

}

It is possible to add more callback, or errback, to a deferred callback.

License

Copyright 2013, Nam T. Nguyen.

The code is released to the public domain.