Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deferred/Promise pattern in C++

I've recently discovered, and fallen in love with, the Deferred/Promise pattern used in jQuery. It just encapsulates so many async use cases, including the wonderful chaining, filtering ability, that I can't believe I missed it for so long.

I've just finished refactoring my AS3 code to use the excellent CodeCatalyst/promise-as3 library (https://github.com/CodeCatalyst/promise-as3), and so started thinking about going back to my C++ code and seeing how I could implement the pattern there.

Before I started coding this myself, I checked to see if it had been done before, and discovered the std::future/std::promise (and boost equivalents), but they are very heavy (they seem use real threads etc, and have a heavy template syntax).

So, my question is: Is there are lightweight, pure C++ implementation of the Deferred/Promise pattern, jQuery-style?

refs:

  • pattern: http://en.wikipedia.org/wiki/Futures_and_promises
  • jQuery: http://api.jquery.com/category/deferred-object/
  • AS3: https://github.com/CodeCatalyst/promise-as3
  • Dart: http://api.dartlang.org/dart_core/Futures.html
  • C++: http://www.stdthread.co.uk/doc/headers/future/future.html
like image 246
Shane Avatar asked Jul 09 '12 05:07

Shane


People also ask

What is a promise in programming?

Promises are a pattern that helps with one particular kind of asynchronous programming: a function (or method) that returns a single result asynchronously. One popular way of receiving such a result is via a callback (“callbacks as continuations”): asyncFunction ( arg1 , arg2 , result => { console . log ( result ); });

What is the difference between future and promise?

A future is a placeholder object for a result that does not yet exist. A promise is a writable, single-assignment container, which completes a future. Promises can complete the future with a result to indicate success, or with an exception to indicate failure.

What is deferred in REST API?

We use deferred promise in SharePoint rest calls to wait for the script execution until we get the response from that rest call.

What is deferred in promise?

The deferred. promise() method allows an asynchronous function to prevent other code from interfering with the progress or status of its internal request.


2 Answers

Sorry to play necromancer, but I too was very interested using A+ style promises in C++ and spent years working out the best way to implement it. I did eventually succeed, and you can see my implementation here.

Usage is pretty straight forward, but does make heavy use of templating and template metaprogramming. Here's an example:

Promise<int> promise;

promise.future().then([](int i){
    std::cout << "i = " << i << std::endl;
    return "foobar";
}).then([](const std::string& str){
    std::cout << "str = " << str << std::endl;
});

promise.resolve(10);

This would print out:

i = 10
str = foobar
like image 95
Oz. Avatar answered Oct 14 '22 12:10

Oz.


I am not sure how lightweight a solution you are after, but std::async simplifies the setting up of future/promise pairs a great deal and allows the caller to decide whether a the work is carried out asynchronously by another thread, or with delayed execution in the same thread. In any case, the caller doesn't have to do any explicit thread management.

like image 25
juanchopanza Avatar answered Oct 14 '22 14:10

juanchopanza