|
Boost : |
From: Chris Fairles (chris.fairles_at_[hidden])
Date: 2008-05-10 15:53:26
There's a nice threadpool library (http://threadpool.sourceforge.net)
mentioned on a previous thread that I took a look at and noticed it
had a basic future impl. of its own. Given that there's a future
library under review, I swapped out the original future impl. in the
schedule function, plus added a lazy_schedule (from the examples @
http://braddock.com/~braddock/future/ ) for fun.
I replaced everything in future.hpp from the threadpool lib with:
/*
* Copyright (c) 2005-2007 Philipp Henkel
*
* Use, modification, and distribution are subject to the
* Boost Software License, Version 1.0. (See accompanying file
* LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*
* http://threadpool.sourceforge.net
*
* Chris Fairles, May 2008 - Modified to replace custom future
implementation with boost-reviewed version
*/
#pragma once
#include <boost/future/future.hpp>
namespace boost { namespace threadpool
{
template<class Pool, class F>
future<typename result_of< F() >::type>
schedule(Pool& pool, F const& task)
{
typedef typename result_of< F() >::type result_type;
promise<result_type> prom;
pool.schedule(future_wrapper<result_type>(task, prom));
return future<result_type> (prom);
}
template<class Pool, class F>
future<typename result_of< F() >::type>
lazy_schedule(Pool& pool, F const& task)
{
typedef typename result_of< F() >::type result_type;
promise<result_type> prom;
future<void> guard = prom.get_needed_future();
guard.add_callback(bind(&Pool::schedule, pool,
future_wrapper<result_type>(task, prom)));
return future<result_type> (prom);
}
} } // namespace boost::threadpool
where #include <boost/future/future.hpp> is the header for the lib
under review (and pardon the pragma windowism).
I haven't tested it throughly but on msvc9, the most trivial of cases
work (including catching exceptions) so YMMV.
Ex.
int task_int_1() {
throw std::exception("task_int_1 exception");
return 1;
}
int task_int_2() {
return 2;
}
int main() {
pool tp;
tp.size_controller().resize(2);
future<int> result1 = schedule(tp, &task_int_1);
future<int> result2 = schedule(tp, &task_int_2);
try {
int val = result1 + result2;
std::cout << "you will never see this value... " << val << std::endl;
} catch (std::exception const& ex) }
std::cerr << ex.what() << std::endl;
}
}
Something to play around with anyway.
Cheers,
Chris
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk