Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Eterfree
ThreadPool
提交
ecfb22f0
T
ThreadPool
项目概览
Eterfree
/
ThreadPool
6 个月 前同步成功
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
T
ThreadPool
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
ecfb22f0
编写于
5月 21, 2022
作者:
独孤过
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Update ThreadPool
上级
81ff53c9
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
46 addition
and
26 deletion
+46
-26
src/Condition.hpp
src/Condition.hpp
+17
-10
src/Core.hpp
src/Core.hpp
+2
-1
src/Queue.hpp
src/Queue.hpp
+3
-1
src/Thread.cpp
src/Thread.cpp
+11
-5
src/Thread.h
src/Thread.h
+4
-2
src/ThreadPool.cpp
src/ThreadPool.cpp
+5
-4
src/ThreadPool.h
src/ThreadPool.h
+2
-1
test/test1.cpp
test/test1.cpp
+2
-2
未找到文件。
src/Condition.hpp
浏览文件 @
ecfb22f0
...
...
@@ -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
()
\
...
...
src/Core.hpp
浏览文件 @
ecfb22f0
...
...
@@ -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 {
...
...
src/Queue.hpp
浏览文件 @
ecfb22f0
...
...
@@ -89,7 +89,9 @@ public:
:
_capacity
(
_capacity
),
_size
(
0
)
{}
auto
capacity
()
const
noexcept
{
return
get
(
_capacity
);
}
{
return
get
(
_capacity
);
}
void
reserve
(
SizeType
_capacity
)
noexcept
{
...
...
src/Thread.cpp
浏览文件 @
ecfb22f0
...
...
@@ -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
;
// 非就绪状态不必通知
...
...
src/Thread.h
浏览文件 @
ecfb22f0
...
...
@@ -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
)
...
...
src/ThreadPool.cpp
浏览文件 @
ecfb22f0
...
...
@@ -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
;
...
...
src/ThreadPool.h
浏览文件 @
ecfb22f0
...
...
@@ -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
);
...
...
test/test1.cpp
浏览文件 @
ecfb22f0
...
...
@@ -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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录