c++ - Implement a function similar to async -


i have function foo returns future. foo register callback called after foo returns.

future<int> foo() {     promise<int> p;     future<int> ret(p.get_future());     thread(bind([] (promise<int> &&p) {         this_thread::sleep_for(chrono::seconds(3));         p.set_value(10);     }, move(p))).detach();     return move(ret); }  int main() {     auto f = foo();     cout << f.get() << endl;     return 0; } 

but seems std::bind forwards rvalue reference lvalue reference can not compiled. there way fix it?


i have write ugly class move promise object:

template<typename t> class promise_forward {     promise<t> promise_;  public:     promise_forward(promise<t> &&p) :          promise_(move(p)) {}      promise_forward(promise_forward<t> &&other) :          promise_(move(other.promise_)) {}      operator promise<t> () {         return move(promise_);     } };  future<int> foo() {     promise<int> p;     future<int> ret(p.get_future());     thread(bind([] (promise<int> &&p) {         this_thread::sleep_for(chrono::seconds(3));         p.set_value(10);     }, promise_forward<int>(move(p)))).detach();     return ret; }  int main() {     auto f = foo();     cout << f.get() << endl;     return 0; } 

you, basically, doesn't need std::bind here (well , believe =)). here quick draft of simplest async task launcher. same yours, but, little more generic: can accept function objects , less intrusive: function objects doesn't know nothing promises or threading @ all.

there may mistakes (i'm quite sure are). and, of course, far far away, std::async implementation (which, generally, more thread launcher, but, ideally, have huge thread management back-end).

#include <thread> #include <future> #include <iostream> #include <chrono>   template< class function, class... args> std::future<typename std::result_of<function(args...)>::type> my_async(function && f, args && ... args) {     typedef typename std::result_of<function(args...)>::type ret_type;      std::promise<ret_type> p;      auto fut = p.get_future();       // lambda in separate variable, improve readability      auto l = [](function && f, args && ... args, std::promise<ret_type> && p)     {         p.set_value(f(args...));     };      std::thread th(l, std::move(f), std::move(args...), std::move(p));      th.detach();      return std::move(fut);  }  int wannarunasync(int i) {     return i; };  int main() {      auto fut = my_async(&wannarunasync, 42);      auto fut2 = my_async([](int i) -> int { return i; }, 42);      std::cout << fut.get() << std::endl;     std::cout << fut2.get() << std::endl;      std::cin.get();      return 0; } 

i able compile , run g++-4.8 , clang++ msvc 2012 , 2013 preview doesn't compiles (probably, due errors).

i've not tested code @ all, careful =) hope helps.


Comments

Popular posts from this blog

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

html - How to style widget with post count different than without post count -

url rewriting - How to redirect a http POST with urlrewritefilter -