Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
csdn_franckjun
Paddle
提交
784a19ec
P
Paddle
项目概览
csdn_franckjun
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
784a19ec
编写于
10月 24, 2018
作者:
X
Xin Pan
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix some thread-safty issue and simplify threadpool
test=develop
上级
2256fae4
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
18 addition
and
39 deletion
+18
-39
paddle/fluid/framework/threadpool.cc
paddle/fluid/framework/threadpool.cc
+14
-17
paddle/fluid/framework/threadpool.h
paddle/fluid/framework/threadpool.h
+3
-21
paddle/fluid/framework/threadpool_test.cc
paddle/fluid/framework/threadpool_test.cc
+1
-1
未找到文件。
paddle/fluid/framework/threadpool.cc
浏览文件 @
784a19ec
...
...
@@ -34,6 +34,11 @@ ThreadPool* ThreadPool::GetInstance() {
return
threadpool_
.
get
();
}
void
ThreadPool
::
Reset
()
{
threadpool_
.
reset
(
nullptr
);
ThreadPool
::
Init
();
}
void
ThreadPool
::
Init
()
{
if
(
threadpool_
.
get
()
==
nullptr
)
{
// TODO(Yancey1989): specify the max threads number
...
...
@@ -59,6 +64,7 @@ ThreadPool::ThreadPool(int num_threads)
ThreadPool
::~
ThreadPool
()
{
{
// notify all threads to stop running
std
::
lock_guard
<
std
::
mutex
>
l
(
mutex_
);
running_
=
false
;
scheduled_
.
notify_all
();
}
...
...
@@ -69,19 +75,18 @@ ThreadPool::~ThreadPool() {
}
}
void
ThreadPool
::
Wait
()
{
std
::
unique_lock
<
std
::
mutex
>
lock
(
mutex_
);
completed_
.
wait
(
lock
,
[
=
]
{
return
Done
()
==
true
;
});
}
void
ThreadPool
::
TaskLoop
()
{
while
(
running_
)
{
while
(
true
)
{
std
::
unique_lock
<
std
::
mutex
>
lock
(
mutex_
);
scheduled_
.
wait
(
lock
,
[
=
]
{
return
!
tasks_
.
empty
()
||
!
running_
;
});
if
(
!
running_
)
{
break
;
scheduled_
.
wait
(
lock
,
[
this
]
{
return
!
this
->
tasks_
.
empty
()
||
!
this
->
running_
;
});
std
::
lock_guard
<
std
::
mutex
>
l
(
mutex_
);
if
(
!
running_
||
tasks_
.
empty
())
{
return
;
}
// pop a task from the task queue
auto
task
=
std
::
move
(
tasks_
.
front
());
tasks_
.
pop
();
...
...
@@ -91,14 +96,6 @@ void ThreadPool::TaskLoop() {
// run the task
task
();
{
std
::
unique_lock
<
std
::
mutex
>
lock
(
mutex_
);
++
idle_threads_
;
if
(
Done
())
{
completed_
.
notify_all
();
}
}
}
}
...
...
paddle/fluid/framework/threadpool.h
浏览文件 @
784a19ec
...
...
@@ -55,16 +55,10 @@ class ThreadPool {
// Returns the singleton of ThreadPool.
static
ThreadPool
*
GetInstance
();
~
ThreadPool
();
// Returns the number of threads created by the constructor.
size_t
Threads
()
const
{
return
total_threads_
;
}
// delete current thread pool and create a new one.
static
void
Reset
();
// Returns the number of currently idle threads.
size_t
IdleThreads
()
{
std
::
unique_lock
<
std
::
mutex
>
lock
(
mutex_
);
return
idle_threads_
;
}
~
ThreadPool
();
// Run pushes a function to the task queue and returns a std::future
// object. To wait for the completion of the task, call
...
...
@@ -94,25 +88,13 @@ class ThreadPool {
});
std
::
future
<
std
::
unique_ptr
<
platform
::
EnforceNotMet
>>
f
=
task
.
get_future
();
tasks_
.
push
(
std
::
move
(
task
));
lock
.
unlock
();
scheduled_
.
notify_one
();
return
f
;
}
// Wait until all the tasks are completed.
void
Wait
();
private:
DISABLE_COPY_AND_ASSIGN
(
ThreadPool
);
// If the task queue is empty and avaialbe is equal to the number of
// threads, means that all tasks are completed. Note: this function
// is not thread-safe. Returns true if all tasks are completed.
// Note: don't delete the data member total_threads_ and use
// threads_.size() instead; because you'd need to lock the mutex
// before accessing threads_.
bool
Done
()
{
return
tasks_
.
empty
()
&&
idle_threads_
==
total_threads_
;
}
// The constructor starts threads to run TaskLoop, which retrieves
// and runs tasks from the queue.
void
TaskLoop
();
...
...
paddle/fluid/framework/threadpool_test.cc
浏览文件 @
784a19ec
...
...
@@ -52,6 +52,6 @@ TEST(ThreadPool, ConcurrentRun) {
for
(
auto
&
t
:
threads
)
{
t
.
join
();
}
pool
->
Wai
t
();
framework
::
ThreadPool
::
Rese
t
();
EXPECT_EQ
(
sum
,
((
n
+
1
)
*
n
)
/
2
);
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录