async/make_request.cpp

This is an example of how make_request may be implemented in case you need to do something simliar.

/* The request helper is what will eventually be called on another thread.
   it does the work, binds the result of that work to the callback creating
   a new callback that takes no arguments, and then pushes that onto some
   queue using a push function that is also passed into it.
 */
template <typename T>
void request_helper(boost::function<T ()> const &f,
  boost::function<void (T)> const &c,
  boost::function<void (std::list<request> &)> const &push)
{
  std::list<request> temp(1, boost::bind(c, f()));
  push(temp);
}

/* The rest of the magic hapens in the make_request function.  It binds the
   work function, the callback that will receive the result, and the push
   function for thread that's currently running (the one make_request has been
   called from) to the request_helper function.  This results in a void ()
   function that can be passed to anything that takes a request.
*/
template <typename T>
request make_request(boost::function<T ()> const &f,
  boost::function<void (T)> const &c)
{
  return boost::bind(&request_helper<T>, f, c,
    thread_specific_request_queue::get_push());
}


SourceForge.net Logo