📄 schedulers.hpp
字号:
/*! \file * \brief Task schedulers. * * This file contains some fundamental task schedulers for the pool class. * A task scheduler is a container for tasks which implements a * scheduling policy. This policy controls in which order the tasks * are processed by the thread pool. The schedulers need not to be thread-safe * because they are used by the pool in thread-safe way. * * Copyright (c) 2005-2006 Philipp Henkel * * Distributed under the Boost Software License, Version 1.0. (See * accompanying file LICENSE_1_0.txt or copy at * http://www.boost.org/LICENSE_1_0.txt) * * http://threadpool.sourceforge.net * */#ifndef THREADPOOL_SCHEDULERS_HPP_INCLUDED#define THREADPOOL_SCHEDULERS_HPP_INCLUDED#include <queue>#include <stack>#include "task_adaptors.hpp"namespace threadpool{/*! \brief Fifo task scheduler. * * The first task to be added to the scheduler will be the first to be removed. * The processing proceeds sequentially in the same order. * Fifo stands for "first in, first out". * * \param Task A function object which implements the operator(). Exceptions are ignored. * */ template <class Task = thread_func> class fifo_scheduler{public: typedef Task task_type; //!< Provides the scheduler's task type. /*! Adds a new task to the scheduler. * \param task The task object. */ void push(const task_type& task) { m_queue.push(task); } /*! Removes the task which should be executed next. */ void pop() { m_queue.pop(); } /*! Gets the task which should be executed next. * \return The task object to be executed. */ const task_type& top() const { return m_queue.front(); } /*! Gets the current number of tasks in the scheduler. * \return The number of tasks. * \remarks Prefer empty() to size() == 0 to check if the scheduler is empty. */ size_t size() const { return m_queue.size(); } /*! Checks if the scheduler is empty. * \return true if the scheduler contains no tasks, false otherwise. * \remarks Is more efficient than size() == 0. */ bool empty() const { return m_queue.empty(); }private: std::queue<task_type> m_queue; //!< Internal task container. };/*! \brief Lifo task scheduler. * * The last task to be added to the scheduler will be the first to be removed. * Lifo stands for "last in, first out". * * \param Task A function object which implements the operator(). Exceptions are ignored. * */ template <class Task = thread_func> class lifo_scheduler{public: typedef Task task_type; //!< Provides the scheduler's task type. /*! Adds a new task to the scheduler. * \param task The task object. */ void push(const task_type& task) { m_stack.push(task); } /*! Removes the task which should be executed next. */ void pop() { m_stack.pop(); } /*! Gets the task which should be executed next. * \return The task object to be executed. */ const task_type& top() const { return m_stack.top(); } /*! Gets the current number of tasks in the scheduler. * \return The number of tasks. * \remarks Prefer empty() to size() == 0 to check if the scheduler is empty. */ size_t size() const { return m_stack.size(); } /*! Checks if the scheduler is empty. * \return true if the scheduler contains no tasks, false otherwise. * \remarks Is more efficient than size() == 0. */ bool empty() const { return m_stack.empty(); }private: std::stack<task_type> m_stack; //!< Internal task container. };/*! \brief Scheduler for prioritized tasks. * * The task with highest priority will be the first to be removed. * It must be possible to compare two tasks using operator<. * * \param Task A function object which implements the operator() and operator<. operator< must be a partial ordering. Exceptions are ignored. * * \see prio_thread_func * */ template <class Task = prio_thread_func> class prio_scheduler{public: typedef Task task_type; //!< Provides the scheduler's task type. /*! Adds a new task to the scheduler. * \param task The task object. */ void push(const task_type& task) { m_queue.push(task); } /*! Removes the task which should be executed next. */ void pop() { m_queue.pop(); } /*! Gets the task which should be executed next. * \return The task object to be executed. */ const task_type& top() const { return m_queue.top(); } /*! Gets the current number of tasks in the scheduler. * \return The number of tasks. * \remarks Prefer empty() to size() == 0 to check if the scheduler is empty. */ size_t size() const { return m_queue.size(); } /*! Checks if the scheduler is empty. * \return true if the scheduler contains no tasks, false otherwise. * \remarks Is more efficient than size() == 0. */ bool empty() const { return m_queue.empty(); }private: std::priority_queue<task_type> m_queue; //!< Internal task container.};} // namespace threadpool#endif // THREADPOOL_SCHEDULERS_HPP_INCLUDED
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -