1 // Copyright (C) 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 // 2014/01 Vicente J. Botet Escriba
7 // first implementation of a thread_executor.
9 #ifndef BOOST_THREAD_THREAD_EXECUTOR_HPP
10 #define BOOST_THREAD_THREAD_EXECUTOR_HPP
12 #include <boost/thread/detail/config.hpp>
13 #include <boost/thread/detail/delete.hpp>
14 #include <boost/thread/detail/move.hpp>
15 #include <boost/thread/executors/work.hpp>
16 #include <boost/thread/executors/executor.hpp>
17 #include <boost/thread/thread_only.hpp>
18 #include <boost/thread/scoped_thread.hpp>
19 #include <boost/thread/csbl/vector.hpp>
20 #include <boost/thread/concurrent_queues/queue_op_status.hpp>
22 #include <boost/config/abi_prefix.hpp>
31 /// type-erasure to store the works to do
32 typedef executors::work work;
34 typedef scoped_thread<> thread_t;
35 typedef csbl::vector<thread_t> threads_type;
36 threads_type threads_;
40 * Effects: try to execute one task.
41 * Returns: whether a task has been executed.
42 * Throws: whatever the current task constructor throws or the task() throws.
44 bool try_executing_one()
50 /// thread_executor is not copyable.
51 BOOST_THREAD_NO_COPYABLE(thread_executor)
54 * \b Effects: creates a inline executor that runs closures immediately.
63 * \b Effects: Waits for closures (if any) to complete, then joins and destroys the threads.
65 * \b Synchronization: The completion of all the closures happen before the completion of the \c thread_executor destructor.
69 // signal to all the worker thread that there will be no more submissions.
71 // all the scoped threads will join before destroying
75 * \b Effects: close the \c thread_executor for submissions.
76 * The loop will work until there is no more closures to run.
80 lock_guard<mutex> lk(mtx_);
85 * \b Returns: whether the pool is closed for submissions.
87 bool closed(lock_guard<mutex>& )
93 lock_guard<mutex> lk(mtx_);
98 * \b Requires: \c Closure is a model of \c Callable(void()) and a model of \c CopyConstructible/MoveConstructible.
100 * \b Effects: The specified \c closure will be scheduled for execution at some point in the future.
101 * If invoked closure throws an exception the \c thread_executor will call \c std::terminate, as is the case with threads.
103 * \b Synchronization: completion of \c closure on a particular thread happens before destruction of thread's thread local variables.
105 * \b Throws: \c sync_queue_is_closed if the thread pool is closed.
106 * Whatever exception that can be throw while storing the closure.
109 #if defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
110 template <typename Closure>
111 void submit(Closure & closure)
113 lock_guard<mutex> lk(mtx_);
114 if (closed(lk)) BOOST_THROW_EXCEPTION( sync_queue_is_closed() );
115 threads_.reserve(threads_.size() + 1);
117 threads_.push_back(thread_t(boost::move(th)));
120 void submit(void (*closure)())
122 lock_guard<mutex> lk(mtx_);
123 if (closed(lk)) BOOST_THROW_EXCEPTION( sync_queue_is_closed() );
124 threads_.reserve(threads_.size() + 1);
126 threads_.push_back(thread_t(boost::move(th)));
129 template <typename Closure>
130 void submit(BOOST_THREAD_FWD_REF(Closure) closure)
132 lock_guard<mutex> lk(mtx_);
133 if (closed(lk)) BOOST_THROW_EXCEPTION( sync_queue_is_closed() );
134 threads_.reserve(threads_.size() + 1);
135 thread th(boost::forward<Closure>(closure));
136 threads_.push_back(thread_t(boost::move(th)));
140 * \b Requires: This must be called from an scheduled task.
142 * \b Effects: reschedule functions until pred()
144 template <typename Pred>
145 bool reschedule_until(Pred const&)
152 using executors::thread_executor;
155 #include <boost/config/abi_suffix.hpp>