提交 ecfb22f0 编写于 作者: 独孤过's avatar 独孤过

Update ThreadPool

上级 81ff53c9
......@@ -63,8 +63,7 @@ public:
Condition& operator=(const Condition&) = delete;
explicit operator bool() const noexcept
{ return valid(); }
explicit operator bool() const noexcept { return valid(); }
bool valid() const noexcept
{
......@@ -102,13 +101,15 @@ public:
bool wait_for(const std::chrono::duration<_Rep, _Period>& _relative);
template <typename _Rep, typename _Period, typename _Predicate>
bool wait_for(const std::chrono::duration<_Rep, _Period>& _relative, _Predicate _predicate);
bool wait_for(const std::chrono::duration<_Rep, _Period>& _relative, \
_Predicate _predicate);
template <typename _Clock, typename _Duration>
bool wait_until(const std::chrono::time_point<_Clock, _Duration>& _absolute);
template <typename _Clock, typename _Duration, typename _Predicate>
bool wait_until(const std::chrono::time_point<_Clock, _Duration>& _absolute, _Predicate _predicate);
bool wait_until(const std::chrono::time_point<_Clock, _Duration>& _absolute, \
_Predicate _predicate);
};
template <typename _Size>
......@@ -145,13 +146,15 @@ void Condition<_Size>::notify_all(Strategy _strategy)
}
template <typename _Size>
void Condition<_Size>::notify(Size _size, Strategy _strategy)
void Condition<_Size>::notify(Size _size, \
Strategy _strategy)
{
std::unique_lock lock(_mutex);
if (_strategy == Strategy::RELAXED)
lock.unlock();
for (decltype(_size) index = 0; index < _size; ++index)
for (decltype(_size) index = 0; \
index < _size; ++index)
_condition.notify_one();
}
......@@ -179,13 +182,15 @@ void Condition<_Size>::notify_all(_Predicate _predicate)
template <typename _Size>
template <typename _Predicate>
void Condition<_Size>::notify(Size _size, _Predicate _predicate)
void Condition<_Size>::notify(Size _size, \
_Predicate _predicate)
{
std::unique_lock lock(_mutex);
if (!_predicate()) return;
lock.unlock();
for (decltype(_size) index = 0; index < _size; ++index)
for (decltype(_size) index = 0; \
index < _size; ++index)
_condition.notify_one();
}
......@@ -217,7 +222,8 @@ bool Condition<_Size>::wait_for(const std::chrono::duration<_Rep, _Period>& _rel
template <typename _Size>
template <typename _Rep, typename _Period, typename _Predicate>
bool Condition<_Size>::wait_for(const std::chrono::duration<_Rep, _Period>& _relative, _Predicate _predicate)
bool Condition<_Size>::wait_for(const std::chrono::duration<_Rep, _Period>& _relative, \
_Predicate _predicate)
{
std::unique_lock lock(_mutex);
return _condition.wait_for(lock, _relative, \
......@@ -235,7 +241,8 @@ bool Condition<_Size>::wait_until(const std::chrono::time_point<_Clock, _Duratio
template <typename _Size>
template <typename _Clock, typename _Duration, typename _Predicate>
bool Condition<_Size>::wait_until(const std::chrono::time_point<_Clock, _Duration>& _absolute, _Predicate _predicate)
bool Condition<_Size>::wait_until(const std::chrono::time_point<_Clock, _Duration>& _absolute, \
_Predicate _predicate)
{
std::unique_lock lock(_mutex);
while (valid() && !_predicate() \
......
......@@ -14,7 +14,8 @@
[[deprecated("The name for this item is deprecated.")]]
#define REPLACEMENT(signature) \
[[deprecated("The name for this item is deprecated. Instead, use the name: " STRING(signature) ".")]]
[[deprecated("The name for this item is deprecated. " \
"Instead, use the name: " STRING(signature) ".")]]
// 自定义名称空间
#define ETERFREE_SPACE_BEGIN namespace eterfree {
......
......@@ -89,7 +89,9 @@ public:
: _capacity(_capacity), _size(0) {}
auto capacity() const noexcept
{ return get(_capacity); }
{
return get(_capacity);
}
void reserve(SizeType _capacity) noexcept
{
......
......@@ -37,7 +37,9 @@ struct Thread::Structure
// 获取线程唯一标识
auto getID() const noexcept
{ return _thread.get_id(); }
{
return _thread.get_id();
}
// 设置状态
void setState(State _state) noexcept
......@@ -98,7 +100,8 @@ void Thread::execute(DataType _data)
_data->_condition.wait(predicate);
// 线程退出通道
while (_data->getValidity() || _data->_condition)
while (_data->getValidity() \
|| _data->_condition)
{
using State = Structure::State;
_data->setState(State::RUNNING);
......@@ -216,7 +219,8 @@ void Thread::destroy()
}
// 配置任务队列与回调函数子
bool Thread::configure(const QueueType& _taskQueue, const Callback& _callback)
bool Thread::configure(const QueueType& _taskQueue, \
const Callback& _callback)
{
// 无任务队列
if (!_taskQueue) return false;
......@@ -234,7 +238,8 @@ bool Thread::configure(const QueueType& _taskQueue, const Callback& _callback)
}
// 配置单任务与回调函数子
bool Thread::configure(const TaskType& _task, const Callback& _callback)
bool Thread::configure(const TaskType& _task, \
const Callback& _callback)
{
// 任务无效
if (!_task) return false;
......@@ -262,7 +267,8 @@ bool Thread::notify()
// 若处于阻塞状态则获取任务
using State = Structure::State;
if (state == State::BLOCKED && setTask(data))
if (state == State::BLOCKED \
&& setTask(data))
state = State::RUNNABLE;
// 非就绪状态不必通知
......
......@@ -129,10 +129,12 @@ public:
void destroy();
// 配置任务队列与回调函数子
bool configure(const QueueType& _taskQueue, const Callback& _callback);
bool configure(const QueueType& _taskQueue, \
const Callback& _callback);
// 配置单任务与回调函数子
bool configure(const TaskType& _task, const Callback& _callback);
bool configure(const TaskType& _task, \
const Callback& _callback);
// 启动线程
REPLACEMENT(notify)
......
......@@ -70,7 +70,8 @@ struct ThreadPool::Structure
bool pushTask(TaskQueue&& _taskQueue);
// 设置线程池容量
void setCapacity(SizeType _capacity, bool _notified = false);
void setCapacity(SizeType _capacity, \
bool _notified = false);
// 获取线程池容量
auto getCapacity() const noexcept
......@@ -163,7 +164,8 @@ bool ThreadPool::Structure::pushTask(TaskQueue&& _taskQueue)
}
// 设置线程池容量
void ThreadPool::Structure::setCapacity(SizeType _capacity, bool _notified)
void ThreadPool::Structure::setCapacity(SizeType _capacity, \
bool _notified)
{
auto capacity = this->_capacity.exchange(_capacity, \
std::memory_order_relaxed);
......@@ -305,8 +307,7 @@ ThreadPool::SizeType ThreadPool::adjust(DataType& _data)
auto capacity = _data->getCapacity();
// 1.删减线程
if (size >= capacity)
return size - capacity;
if (size >= capacity) return size - capacity;
// 2.增加线程
size = capacity - size;
......
......@@ -71,7 +71,8 @@ private:
private:
// 创建线程池
static void create(DataType&& _data, SizeType _capacity);
static void create(DataType&& _data, \
SizeType _capacity);
// 销毁线程池
static void destroy(DataType&& _data);
......
......@@ -58,8 +58,8 @@ static void terminate(ThreadPool&& _threadPool)
static auto getConcurrency() noexcept
{
auto concurrency = std::thread::hardware_concurrency();
return concurrency > 0 ? \
concurrency : static_cast<decltype(concurrency)>(1);
return concurrency > 0 ? concurrency \
: static_cast<decltype(concurrency)>(1);
}
using ThreadPool = boost::threadpool::thread_pool<>;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册