future.hpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*! \file
  2. * \brief TODO.
  3. *
  4. * TODO.
  5. *
  6. * Copyright (c) 2005-2007 Philipp Henkel
  7. *
  8. * Use, modification, and distribution are subject to the
  9. * Boost Software License, Version 1.0. (See accompanying file
  10. * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  11. *
  12. * http://threadpool.sourceforge.net
  13. *
  14. */
  15. #ifndef THREADPOOL_FUTURE_HPP_INCLUDED
  16. #define THREADPOOL_FUTURE_HPP_INCLUDED
  17. #include "./detail/future.hpp"
  18. #include <boost/utility/enable_if.hpp>
  19. //#include "pool.hpp"
  20. //#include <boost/utility.hpp>
  21. //#include <boost/thread/mutex.hpp>
  22. namespace boost { namespace threadpool
  23. {
  24. /*! \brief Experimental. Do not use in production code. TODO.
  25. *
  26. * TODO Future
  27. *
  28. * \see TODO
  29. *
  30. */
  31. template<class Result>
  32. class future
  33. {
  34. private:
  35. shared_ptr<detail::future_impl<Result> > m_impl;
  36. public:
  37. typedef Result const & result_type; //!< Indicates the functor's result type.
  38. typedef Result future_result_type; //!< Indicates the future's result type.
  39. public:
  40. future()
  41. : m_impl(new detail::future_impl<future_result_type>()) // TODO remove this
  42. {
  43. }
  44. // only for internal usage
  45. future(shared_ptr<detail::future_impl<Result> > const & impl)
  46. : m_impl(impl)
  47. {
  48. }
  49. bool ready() const
  50. {
  51. return m_impl->ready();
  52. }
  53. void wait() const
  54. {
  55. m_impl->wait();
  56. }
  57. bool timed_wait(boost::xtime const & timestamp) const
  58. {
  59. return m_impl->timed_wait(timestamp);
  60. }
  61. result_type operator()() // throw( thread::cancelation_exception, ... )
  62. {
  63. return (*m_impl)();
  64. }
  65. result_type get() // throw( thread::cancelation_exception, ... )
  66. {
  67. return (*m_impl)();
  68. }
  69. bool cancel()
  70. {
  71. return m_impl->cancel();
  72. }
  73. bool is_cancelled() const
  74. {
  75. return m_impl->is_cancelled();
  76. }
  77. };
  78. template<class Pool, class Function>
  79. typename disable_if <
  80. is_void< typename result_of< Function() >::type >,
  81. future< typename result_of< Function() >::type >
  82. >::type
  83. schedule(Pool& pool, const Function& task)
  84. {
  85. typedef typename result_of< Function() >::type future_result_type;
  86. // create future impl and future
  87. shared_ptr<detail::future_impl<future_result_type> > impl(new detail::future_impl<future_result_type>);
  88. future <future_result_type> res(impl);
  89. // schedule future impl
  90. pool.schedule(detail::future_impl_task_func<detail::future_impl, Function>(task, impl));
  91. // return future
  92. return res;
  93. /*
  94. TODO
  95. if(pool->schedule(bind(&Future::run, future)))
  96. {
  97. return future;
  98. }
  99. else
  100. {
  101. // construct empty future
  102. return error_future;
  103. }
  104. */
  105. }
  106. } } // namespace boost::threadpool
  107. #endif // THREADPOOL_FUTURE_HPP_INCLUDED