pool_adaptors.hpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*! \file
  2. * \brief Pool adaptors.
  3. *
  4. * This file contains an easy-to-use adaptor similar to a smart
  5. * pointer for the pool class.
  6. *
  7. * Copyright (c) 2005-2007 Philipp Henkel
  8. *
  9. * Use, modification, and distribution are subject to the
  10. * Boost Software License, Version 1.0. (See accompanying file
  11. * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  12. *
  13. * http://threadpool.sourceforge.net
  14. *
  15. */
  16. #ifndef THREADPOOL_POOL_ADAPTORS_HPP_INCLUDED
  17. #define THREADPOOL_POOL_ADAPTORS_HPP_INCLUDED
  18. #include <boost/smart_ptr.hpp>
  19. namespace boost { namespace threadpool
  20. {
  21. // TODO convenience scheduling function
  22. /*! Schedules a Runnable for asynchronous execution. A Runnable is an arbitrary class with a run()
  23. * member function. This a convenience shorthand for pool->schedule(bind(&Runnable::run, task_object)).
  24. * \param
  25. * \param obj The Runnable object. The member function run() will be exectued and should not throw execeptions.
  26. * \return true, if the task could be scheduled and false otherwise.
  27. */
  28. template<typename Pool, typename Runnable>
  29. bool schedule(Pool& pool, shared_ptr<Runnable> const & obj)
  30. {
  31. return pool->schedule(bind(&Runnable::run, obj));
  32. }
  33. /*! Schedules a task for asynchronous execution. The task will be executed once only.
  34. * \param task The task function object.
  35. */
  36. template<typename Pool>
  37. typename enable_if <
  38. is_void< typename result_of< typename Pool::task_type() >::type >,
  39. bool
  40. >::type
  41. schedule(Pool& pool, typename Pool::task_type const & task)
  42. {
  43. return pool.schedule(task);
  44. }
  45. template<typename Pool>
  46. typename enable_if <
  47. is_void< typename result_of< typename Pool::task_type() >::type >,
  48. bool
  49. >::type
  50. schedule(shared_ptr<Pool> const pool, typename Pool::task_type const & task)
  51. {
  52. return pool->schedule(task);
  53. }
  54. } } // namespace boost::threadpool
  55. #endif // THREADPOOL_POOL_ADAPTORS_HPP_INCLUDED