Sunday, July 14, 2013

Is a Promise actually a Wish in c++11

A typical usage of promise is like blow:

// future from a promise
std::promise<int> p;
std::future<int> f3 = p.get_future();
std::thread( [](std::promise<int>& p){ p.set_value(9); }, 
            std::ref(p) ).detach();
f3.wait();

It's confusing, because in real life a promise is what you are willing to do for others rather than asking others to do.
If we call promise here a wish, it all makes sense:
One makes a wish, then gives the wish to another(a thread) and waits for the wish to come true. The wish may not come true because the thread can throw a exception rather than set_value.

1 comment:

  1. If you rename std::promise to std::wish, then you have to rename the best-named C++ class ever, std::broken_promise

    ReplyDelete