Boost logo

Boost Users :

Subject: [Boost-users] [threadpool] Using with Boost.Bind
From: Robert Jones (robertgbjones_at_[hidden])
Date: 2011-10-14 09:41:39


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



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net