1 // Copyright (C) 2013,2014 Vicente J. Botet Escriba
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 // 2013/09 Vicente J. Botet Escriba
7 // Adapt to boost from CCIA C++11 implementation
9 #ifndef BOOST_THREAD_EXECUTORS_EXECUTOR_ADAPTOR_HPP
10 #define BOOST_THREAD_EXECUTORS_EXECUTOR_ADAPTOR_HPP
12 #include <boost/thread/detail/config.hpp>
14 #include <boost/thread/executors/executor.hpp>
16 #include <boost/config/abi_prefix.hpp>
23 * Polymorphic adaptor of a model of Executor to an executor.
25 template <typename Executor>
26 class executor_adaptor : public executor
30 /// type-erasure to store the works to do
31 typedef executor::work work;
33 /// executor is not copyable.
34 BOOST_THREAD_NO_COPYABLE(executor_adaptor)
37 * executor_adaptor constructor
39 #if ! defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
40 template <typename ...Args>
41 executor_adaptor(BOOST_THREAD_RV_REF(Args) ... args) : ex(boost::forward<Args>(args)...) {}
44 * executor_adaptor constructor
46 executor_adaptor() : ex() {}
48 template <typename A1>
50 BOOST_THREAD_FWD_REF(A1) a1
53 boost::forward<A1>(a1)
55 template <typename A1, typename A2>
57 BOOST_THREAD_FWD_REF(A1) a1,
58 BOOST_THREAD_FWD_REF(A2) a2
61 boost::forward<A1>(a1),
62 boost::forward<A2>(a2)
64 template <typename A1, typename A2, typename A3>
66 BOOST_THREAD_FWD_REF(A1) a1,
67 BOOST_THREAD_FWD_REF(A2) a2,
68 BOOST_THREAD_FWD_REF(A3) a3
71 boost::forward<A1>(a1),
72 boost::forward<A2>(a2),
73 boost::forward<A3>(a3)
76 Executor& underlying_executor() { return ex; }
79 * \b Effects: close the \c executor for submissions.
80 * The worker threads will work until there is no more closures to run.
82 void close() { ex.close(); }
85 * \b Returns: whether the pool is closed for submissions.
87 bool closed() { return ex.closed(); }
90 * \b Effects: The specified closure will be scheduled for execution at some point in the future.
91 * If invoked closure throws an exception the executor will call std::terminate, as is the case with threads.
93 * \b Synchronization: completion of closure on a particular thread happens before destruction of thread's thread local variables.
95 * \b Throws: \c sync_queue_is_closed if the thread pool is closed.
96 * Whatever exception that can be throw while storing the closure.
98 void submit(BOOST_THREAD_RV_REF(work) closure) {
99 return ex.submit(boost::move(closure));
102 #if defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
103 template <typename Closure>
104 void submit(Closure & closure)
106 submit(work(closure));
109 void submit(void (*closure)())
111 submit(work(closure));
114 template <typename Closure>
115 void submit(BOOST_THREAD_FWD_REF(Closure) closure)
117 //submit(work(boost::forward<Closure>(closure)));
118 work w((boost::forward<Closure>(closure)));
119 submit(boost::move(w));
123 * Effects: try to execute one task.
124 * Returns: whether a task has been executed.
125 * Throws: whatever the current task constructor throws or the task() throws.
127 bool try_executing_one() { return ex.try_executing_one(); }
131 using executors::executor_adaptor;
134 #include <boost/config/abi_suffix.hpp>