Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
84994608
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2298
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
84994608
编写于
2月 06, 2018
作者:
Y
Yu Yang
提交者:
GitHub
2月 06, 2018
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #8181 from reyoung/feature/add_exception_for_thread_pool
Add RunAndGetException in threadpool
上级
a07b7510
c966c281
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
40 addition
and
4 deletion
+40
-4
paddle/framework/threadpool.h
paddle/framework/threadpool.h
+40
-4
未找到文件。
paddle/framework/threadpool.h
浏览文件 @
84994608
...
@@ -21,7 +21,8 @@ limitations under the License. */
...
@@ -21,7 +21,8 @@ limitations under the License. */
#include <queue>
#include <queue>
#include <thread>
#include <thread>
#include <vector>
#include <vector>
#include "glog/logging.h"
#include "paddle/platform/enforce.h"
#include "paddle/platform/macros.h" // for DISABLE_COPY_AND_ASSIGN
#include "paddle/platform/macros.h" // for DISABLE_COPY_AND_ASSIGN
namespace
paddle
{
namespace
paddle
{
...
@@ -31,7 +32,7 @@ namespace framework {
...
@@ -31,7 +32,7 @@ namespace framework {
// number of threads.
// number of threads.
class
ThreadPool
{
class
ThreadPool
{
public:
public:
typedef
std
::
packaged_task
<
void
()
>
Task
;
using
Task
=
std
::
packaged_task
<
std
::
unique_ptr
<
platform
::
EnforceNotMet
>
()
>
;
// Returns the singleton of ThreadPool.
// Returns the singleton of ThreadPool.
static
ThreadPool
*
GetInstance
();
static
ThreadPool
*
GetInstance
();
...
@@ -52,9 +53,28 @@ class ThreadPool {
...
@@ -52,9 +53,28 @@ class ThreadPool {
// std::future::wait().
// std::future::wait().
template
<
typename
Callback
>
template
<
typename
Callback
>
std
::
future
<
void
>
Run
(
Callback
fn
)
{
std
::
future
<
void
>
Run
(
Callback
fn
)
{
auto
f
=
this
->
RunAndGetException
(
fn
);
return
std
::
async
(
std
::
launch
::
deferred
,
ExceptionHandler
(
std
::
move
(
f
)));
}
template
<
typename
Callback
>
std
::
future
<
std
::
unique_ptr
<
platform
::
EnforceNotMet
>>
RunAndGetException
(
Callback
fn
)
{
std
::
unique_lock
<
std
::
mutex
>
lock
(
mutex_
);
std
::
unique_lock
<
std
::
mutex
>
lock
(
mutex_
);
Task
task
(
std
::
bind
(
fn
));
Task
task
([
fn
]()
->
std
::
unique_ptr
<
platform
::
EnforceNotMet
>
{
std
::
future
<
void
>
f
=
task
.
get_future
();
try
{
fn
();
return
nullptr
;
}
catch
(
platform
::
EnforceNotMet
ex
)
{
return
std
::
unique_ptr
<
platform
::
EnforceNotMet
>
(
new
platform
::
EnforceNotMet
(
ex
));
}
catch
(...)
{
LOG
(
FATAL
)
<<
"Unexpected exception is catched in thread pool. All "
"throwable exception in Fluid should be an EnforceNotMet."
;
}
});
std
::
future
<
std
::
unique_ptr
<
platform
::
EnforceNotMet
>>
f
=
task
.
get_future
();
tasks_
.
push
(
std
::
move
(
task
));
tasks_
.
push
(
std
::
move
(
task
));
lock
.
unlock
();
lock
.
unlock
();
scheduled_
.
notify_one
();
scheduled_
.
notify_one
();
...
@@ -65,6 +85,22 @@ class ThreadPool {
...
@@ -65,6 +85,22 @@ class ThreadPool {
void
Wait
();
void
Wait
();
private:
private:
struct
ExceptionHandler
{
mutable
std
::
future
<
std
::
unique_ptr
<
platform
::
EnforceNotMet
>>
future_
;
explicit
ExceptionHandler
(
std
::
future
<
std
::
unique_ptr
<
platform
::
EnforceNotMet
>>&&
f
)
:
future_
(
std
::
move
(
f
))
{}
void
operator
()()
const
{
auto
ex
=
this
->
future_
.
get
();
if
(
ex
!=
nullptr
)
{
LOG
(
FATAL
)
<<
"The exception is thrown inside the thread pool. You "
"should use RunAndGetException to handle the exception.
\n
"
"The default exception handler is LOG(FATAL)."
<<
ex
->
what
();
}
}
};
DISABLE_COPY_AND_ASSIGN
(
ThreadPool
);
DISABLE_COPY_AND_ASSIGN
(
ThreadPool
);
explicit
ThreadPool
(
int
num_threads
);
explicit
ThreadPool
(
int
num_threads
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录