00001 00016 #ifndef THREADPOOL_FUTURE_HPP_INCLUDED 00017 #define THREADPOOL_FUTURE_HPP_INCLUDED 00018 00019 00020 00021 #include "./detail/future.hpp" 00022 #include <boost/utility/enable_if.hpp> 00023 00024 //#include "pool.hpp" 00025 //#include <boost/utility.hpp> 00026 00027 //#include <boost/thread/mutex.hpp> 00028 00029 00030 namespace boost { namespace threadpool 00031 { 00032 00042 template<class Result> 00043 class future 00044 { 00045 private: 00046 shared_ptr<detail::future_impl<Result> > m_impl; 00047 00048 public: 00049 typedef Result const & result_type; 00050 typedef Result future_result_type; 00051 00052 00053 public: 00054 00055 future() 00056 : m_impl(new detail::future_impl<future_result_type>()) // TODO remove this 00057 { 00058 } 00059 00060 // only for internal usage 00061 future(shared_ptr<detail::future_impl<Result> > const & impl) 00062 : m_impl(impl) 00063 { 00064 } 00065 00066 bool ready() const 00067 { 00068 return m_impl->ready(); 00069 } 00070 00071 void wait() const 00072 { 00073 m_impl->wait(); 00074 } 00075 00076 bool timed_wait(boost::xtime const & timestamp) const 00077 { 00078 return m_impl->timed_wait(timestamp); 00079 } 00080 00081 result_type operator()() // throw( thread::cancelation_exception, ... ) 00082 { 00083 return (*m_impl)(); 00084 } 00085 00086 result_type get() // throw( thread::cancelation_exception, ... ) 00087 { 00088 return (*m_impl)(); 00089 } 00090 00091 bool cancel() 00092 { 00093 return m_impl->cancel(); 00094 } 00095 00096 bool is_cancelled() const 00097 { 00098 return m_impl->is_cancelled(); 00099 } 00100 }; 00101 00102 00103 00104 00105 00106 template<class Pool, class Function> 00107 typename disable_if < 00108 is_void< typename result_of< Function() >::type >, 00109 future< typename result_of< Function() >::type > 00110 >::type 00111 schedule(Pool& pool, const Function& task) 00112 { 00113 typedef typename result_of< Function() >::type future_result_type; 00114 00115 // create future impl and future 00116 shared_ptr<detail::future_impl<future_result_type> > impl(new detail::future_impl<future_result_type>); 00117 future <future_result_type> res(impl); 00118 00119 // schedule future impl 00120 pool.schedule(detail::future_impl_task_func<detail::future_impl, Function>(task, impl)); 00121 00122 // return future 00123 return res; 00124 00125 /* 00126 TODO 00127 if(pool->schedule(bind(&Future::run, future))) 00128 { 00129 return future; 00130 } 00131 else 00132 { 00133 // construct empty future 00134 return error_future; 00135 } 00136 */ 00137 } 00138 00139 00140 00141 } } // namespace boost::threadpool 00142 00143 #endif // THREADPOOL_FUTURE_HPP_INCLUDED 00144