ios task worker

Programming Skill 2016. 8. 24. 12:45
#include <iostream>
#include <future>
#include <type_traits>
#include <boost/asio.hpp>
 
using namespace boost::asio;
 
io_service ios;
io_service::work work{ ios };
 
template < typename Func >
auto post(Func&& func)
{
  using return_type = decltype(func());
  
  auto promise = std::make_shared<std::promise<return_type>>();
  auto future = promise->get_future();
 
  ios.post
    (
      [promise, func]
      {
        promise->set_value(func());
      }
    );
 
  return future;
}
 
 
int main()
{
  auto async_res = std::async
    (
      std::launch::async, 
      [] 
      {
        ios.run();
      }
    );
 
  int a = 10;
  int b = 20;
 
  auto future = post([a, b] { return a*b; });
 
  std::cout << future.get() << std::endl;
 
  return 0;
}



출처 : http://zepeh.tistory.com/315


Posted by 명혀니
,