summaryrefslogtreecommitdiff
path: root/boost/thread/executors/loop_executor.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'boost/thread/executors/loop_executor.hpp')
-rw-r--r--boost/thread/executors/loop_executor.hpp40
1 files changed, 22 insertions, 18 deletions
diff --git a/boost/thread/executors/loop_executor.hpp b/boost/thread/executors/loop_executor.hpp
index b08ff76f57..aee7a8d477 100644
--- a/boost/thread/executors/loop_executor.hpp
+++ b/boost/thread/executors/loop_executor.hpp
@@ -16,6 +16,7 @@
#include <boost/thread/detail/move.hpp>
#include <boost/thread/concurrent_queues/sync_queue.hpp>
#include <boost/thread/executors/work.hpp>
+#include <boost/assert.hpp>
#include <boost/config/abi_prefix.hpp>
@@ -41,14 +42,32 @@ namespace executors
*/
bool try_executing_one()
{
+ return execute_one(/*wait:*/false);
+ }
+
+ private:
+ /**
+ * Effects: Execute one task.
+ * Remark: If wait is true, waits until a task is available or the executor
+ * is closed. If wait is false, returns false immediately if no
+ * task is available.
+ * Returns: whether a task has been executed (if wait is true, only returns false if closed).
+ * Throws: whatever the current task constructor throws or the task() throws.
+ */
+ bool execute_one(bool wait)
+ {
work task;
try
{
- if (work_queue.try_pull(task) == queue_op_status::success)
+ queue_op_status status = wait ?
+ work_queue.wait_pull(task) :
+ work_queue.try_pull(task);
+ if (status == queue_op_status::success)
{
task();
return true;
}
+ BOOST_ASSERT(!wait || status == queue_op_status::closed);
return false;
}
catch (...)
@@ -57,21 +76,6 @@ namespace executors
//return false;
}
}
- private:
- /**
- * Effects: schedule one task or yields
- * Throws: whatever the current task constructor throws or the task() throws.
- */
- void schedule_one_or_yield()
- {
- if ( ! try_executing_one())
- {
- this_thread::yield();
- }
- }
-
-
-
public:
/// loop_executor is not copyable.
@@ -101,10 +105,10 @@ namespace executors
*/
void loop()
{
- while (!closed())
+ while (execute_one(/*wait:*/true))
{
- schedule_one_or_yield();
}
+ BOOST_ASSERT(closed());
while (try_executing_one())
{
}