[threadpool] Using with Boost.Bind
Hi All I'm making use of a threadpool implementation from sourceforge, which I know is not properly part of Boost, but I'm hoping someone has some experience which can help me. I'm trying to use one of the functions from pool_adaptors.hpp, which I've reproduced below. I think this contains an error in the first template as the parameter passed as a Pool is used as a pointer to Pool, and I'm guessing there should four functions in here, not three, as the set seems incomplete. On either count I might be missing something. Stripped down my code says approximately this... #include "threadpool.hpp" #include "boost/threadpool/pool_adaptors.hpp" using namespace boost::threadpool; void task( int ); vector<int> v; v += 1,2,3,4,5,6,7,8,9; pool my_pool; for_each( v.begin( ), v.end( ), bind( schedule, my_pool, _1 ) ); ...and of course the problem is that 'schedule' is both templated and overloaded, so the compiler complains it can't find/resolve 'schedule'. The usual solution is to express the exact type of 'schedule' required, but it's huge, and uses types internal the the threadpool library. Is there a better solution? Thanks - Rob. namespace boost { namespace threadpool { // TODO convenience scheduling function /*! Schedules a Runnable for asynchronous execution. A Runnable is an arbitrary class with a run() * member function. This a convenience shorthand for pool->schedule(bind(&Runnable::run, task_object)). * \param * \param obj The Runnable object. The member function run() will be exectued and should not throw execeptions. * \return true, if the task could be scheduled and false otherwise. */ template<typename Pool, typename Runnable> bool schedule(Pool& pool, shared_ptr<Runnable> const & obj) { return pool->schedule(bind(&Runnable::run, obj)); } /*! Schedules a task for asynchronous execution. The task will be executed once only. * \param task The task function object. */ template<typename Pool> typename enable_if < is_void< typename result_of< typename Pool::task_type() >::type >, bool >::type schedule(Pool& pool, typename Pool::task_type const & task) { return pool.schedule(task); } template<typename Pool> typename enable_if < is_void< typename result_of< typename Pool::task_type() >::type >, bool >::type schedule(shared_ptr<Pool> const pool, typename Pool::task_type const & task) { return pool->schedule(task); } } } // namespace boost::threadpool
Robert Jones wrote: ...
void task( int );
vector<int> v; v += 1,2,3,4,5,6,7,8,9; pool my_pool;
for_each( v.begin( ), v.end( ), bind( schedule, my_pool, _1 ) );
The easiest solution would be for you to define void my_schedule( pool & my_pool, int k ) { schedule( pool, boost::bind( task, k ) ); } and then use my_schedule above (you'll need ref(my_pool) though.) Or, you could bind &pool::schedule directly, without going via the schedule convenience wrapper: bind( &pool::schedule, &my_pool, protect( bind( task, _1 ) ) )
On Fri, Oct 14, 2011 at 8:46 PM, Peter Dimov <pdimov@pdimov.com> wrote:
Robert Jones wrote: ...
void task( int );
vector<int> v; v += 1,2,3,4,5,6,7,8,9; pool my_pool;
for_each( v.begin( ), v.end( ), bind( schedule, my_pool, _1 ) );
The easiest solution would be for you to define
void my_schedule( pool & my_pool, int k ) { schedule( pool, boost::bind( task, k ) ); }
and then use my_schedule above (you'll need ref(my_pool) though.)
Or, you could bind &pool::schedule directly, without going via the schedule convenience wrapper:
bind( &pool::schedule, &my_pool, protect( bind( task, _1 ) ) )
Hi Peter Thanks for that - I very much prefer your second solution as it keeps it all very local, and thanks also for deciphering my gibberish which did of course completely fail to mention 'task()'! - Rob.
participants (2)
-
Peter Dimov
-
Robert Jones