Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Pinoxchio
apollo
提交
4dcbf9a6
A
apollo
项目概览
Pinoxchio
/
apollo
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
A
apollo
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
4dcbf9a6
编写于
12月 30, 2017
作者:
Z
Zhang Liangliang
提交者:
Liangliang Zhang
12月 30, 2017
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
common: fixed lint error in ctpl_stl threadpool
上级
6f5a4a12
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
35 addition
and
19 deletion
+35
-19
modules/common/util/ctpl_stl.h
modules/common/util/ctpl_stl.h
+14
-13
modules/common/util/ctpl_stl_test.cc
modules/common/util/ctpl_stl_test.cc
+21
-6
未找到文件。
modules/common/util/ctpl_stl.h
浏览文件 @
4dcbf9a6
...
...
@@ -21,8 +21,8 @@
// This file is a modification of
// https://github.com/vit-vit/CTPL/blob/master/ctpl_stl.h
#ifndef
__ctpl_stl_thread_pool_H_
_
#define
__ctpl_stl_thread_pool_H_
_
#ifndef
MODULES_COMMON_UTIL_CTPL_STL_H
_
#define
MODULES_COMMON_UTIL_CTPL_STL_H
_
#include <atomic>
#include <exception>
...
...
@@ -32,6 +32,7 @@
#include <mutex>
#include <queue>
#include <thread>
#include <utility>
#include <vector>
// thread pool to run user's functors with signature
...
...
@@ -53,7 +54,7 @@ class Queue {
return
true
;
}
// deletes the retrieved element, do not use for non integral types
bool
pop
(
T
&
v
)
{
bool
pop
(
T
&
v
)
{
// NOLINT
std
::
unique_lock
<
std
::
mutex
>
lock
(
this
->
mutex
);
if
(
this
->
q
.
empty
())
return
false
;
v
=
this
->
q
.
front
();
...
...
@@ -69,18 +70,18 @@ class Queue {
std
::
queue
<
T
>
q
;
std
::
mutex
mutex
;
};
}
}
// namespace detail
class
thread_p
ool
{
class
ThreadP
ool
{
public:
thread_p
ool
()
{
this
->
init
();
}
thread_p
ool
(
int
nThreads
)
{
ThreadP
ool
()
{
this
->
init
();
}
explicit
ThreadP
ool
(
int
nThreads
)
{
this
->
init
();
this
->
resize
(
nThreads
);
}
// the destructor waits for all the functions in the queue to be finished
~
thread_p
ool
()
{
this
->
stop
(
true
);
}
~
ThreadP
ool
()
{
this
->
stop
(
true
);
}
// get the number of running threads in the pool
int
size
()
{
return
static_cast
<
int
>
(
this
->
threads
.
size
());
}
...
...
@@ -202,10 +203,10 @@ class thread_pool {
private:
// deleted
thread_pool
(
const
thread_p
ool
&
);
// = delete;
thread_pool
(
thread_p
ool
&&
);
// = delete;
thread_pool
&
operator
=
(
const
thread_p
ool
&
);
// = delete;
thread_pool
&
operator
=
(
thread_p
ool
&&
);
// = delete;
ThreadPool
(
const
ThreadP
ool
&
);
// = delete;
ThreadPool
(
ThreadP
ool
&&
);
// = delete;
ThreadPool
&
operator
=
(
const
ThreadP
ool
&
);
// = delete;
ThreadPool
&
operator
=
(
ThreadP
ool
&&
);
// = delete;
void
set_thread
(
int
i
)
{
std
::
shared_ptr
<
std
::
atomic
<
bool
>>
flag
(
...
...
@@ -264,4 +265,4 @@ class thread_pool {
}
// namespace common
}
// namespace apollo
#endif //
__ctpl_stl_thread_pool_H_
_
#endif //
MODULES_COMMON_UTIL_CTPL_STL_H
_
modules/common/util/ctpl_stl_test.cc
浏览文件 @
4dcbf9a6
...
...
@@ -16,6 +16,8 @@
#include "modules/common/util/ctpl_stl.h"
#include <atomic>
#include "gtest/gtest.h"
namespace
apollo
{
...
...
@@ -23,17 +25,30 @@ namespace common {
namespace
util
{
namespace
{
void
simple_print
(
int
id
)
{
std
::
cout
<<
"hello from "
<<
id
<<
", function
\n
"
;
}
std
::
atomic
<
int
>
n
(
0
);
void
simple_add
()
{
n
++
;
}
void
simple_minus
()
{
n
--
;
}
}
TEST
(
ThreadPool
,
simple
)
{
thread_pool
p
(
5
);
for
(
int
i
=
0
;
i
<
10
;
++
i
)
{
auto
f1
=
std
::
bind
(
simple_print
,
i
);
ThreadPool
p
(
5
);
for
(
int
i
=
0
;
i
<
1000
;
++
i
)
{
auto
f1
=
std
::
bind
(
simple_add
);
p
.
push
(
f1
);
}
p
.
stop
(
true
);
EXPECT_EQ
(
n
.
load
(),
1000
);
for
(
int
i
=
0
;
i
<
500
;
++
i
)
{
auto
f1
=
std
::
bind
(
simple_add
);
auto
f2
=
std
::
bind
(
simple_minus
);
p
.
push
(
f1
);
p
.
push
(
f2
);
}
p
.
stop
(
true
);
EXPECT_EQ
(
n
.
load
(),
1000
);
}
}
// namespace util
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录