mardi 24 mars 2015

std::async vs std::promise

I have to wrap a getter function into a std::future object. So simple question, what is the best / fastest way to do this?


Here are two options I came up with.


I have a function:



std::function<String (String)> getter;


Then wrap this using std::promise:



std::function<std::future<String> (String)> binding = [getter](String context) {
std::promise<String> p;
p.set_value(getter(contex));
return p.get_future();
};


Or using std::async:



std::function<std::future<String> (String)> binding = [getter](String context) {
return std::async(std::launch::deferred, getter, contex);
};

Aucun commentaire:

Enregistrer un commentaire