Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
2dot5
ClickHouse
提交
1bb6ccd7
C
ClickHouse
项目概览
2dot5
/
ClickHouse
通知
3
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
C
ClickHouse
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
1bb6ccd7
编写于
8月 13, 2016
作者:
A
Alexey Milovidov
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Better [#METR-22410].
上级
9efc240f
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
126 addition
and
99 deletion
+126
-99
dbms/CMakeLists.txt
dbms/CMakeLists.txt
+1
-0
dbms/include/DB/Common/ThreadPool.h
dbms/include/DB/Common/ThreadPool.h
+22
-99
dbms/src/Common/ThreadPool.cpp
dbms/src/Common/ThreadPool.cpp
+103
-0
未找到文件。
dbms/CMakeLists.txt
浏览文件 @
1bb6ccd7
...
...
@@ -602,6 +602,7 @@ add_library (dbms
src/Common/getNumberOfPhysicalCPUCores.cpp
src/Common/StringUtils.cpp
src/Common/randomSeed.cpp
src/Common/ThreadPool.cpp
src/Core/Field.cpp
src/Core/FieldVisitors.cpp
...
...
dbms/include/DB/Common/ThreadPool.h
浏览文件 @
1bb6ccd7
...
...
@@ -20,59 +20,27 @@ private:
using
Job
=
std
::
function
<
void
()
>
;
public:
ThreadPool
(
size_t
m_size
)
:
m_size
(
m_size
)
{
threads
.
reserve
(
m_size
);
for
(
size_t
i
=
0
;
i
<
m_size
;
++
i
)
threads
.
emplace_back
([
this
]
{
worker
();
});
}
void
schedule
(
Job
job
)
{
{
std
::
unique_lock
<
std
::
mutex
>
lock
(
mutex
);
has_free_thread
.
wait
(
lock
,
[
this
]
{
return
active_jobs
<
m_size
||
shutdown
;
});
if
(
shutdown
)
return
;
jobs
.
push
(
std
::
move
(
job
));
++
active_jobs
;
}
has_new_job_or_shutdown
.
notify_one
();
}
void
wait
()
{
{
std
::
unique_lock
<
std
::
mutex
>
lock
(
mutex
);
has_free_thread
.
wait
(
lock
,
[
this
]
{
return
active_jobs
==
0
||
shutdown
;
});
if
(
!
exceptions
.
empty
())
std
::
rethrow_exception
(
exceptions
.
front
());
}
}
~
ThreadPool
()
{
{
std
::
unique_lock
<
std
::
mutex
>
lock
(
mutex
);
shutdown
=
true
;
}
has_new_job_or_shutdown
.
notify_all
();
for
(
auto
&
thread
:
threads
)
thread
.
join
();
}
/// Size is constant, all threads are created immediately.
ThreadPool
(
size_t
m_size
);
/// Add new job. Locks until free thread in pool become available or exception in one of threads was thrown.
/// If an exception in some thread was thrown, method silently returns, and exception will be rethrown only on call to 'wait' function.
void
schedule
(
Job
job
);
/// Wait for all currently active jobs to be done.
/// You may call schedule and wait many times in arbitary order.
/// If any thread was throw an exception, it will be rethrown from this method.
/// List of exceptions is not cleared: on subsequent calls to wait, same exception will be rethrown.
void
wait
();
/// Waits for all threads. Doesn't rethrow exceptions (use 'wait' method to rethrow exceptions).
/// You should not destroy object while calling schedule or wait methods from another threads.
~
ThreadPool
();
size_t
size
()
const
{
return
m_size
;
}
size_t
active
()
const
{
std
::
unique_lock
<
std
::
mutex
>
lock
(
mutex
);
return
active_jobs
;
}
/// Returns number of active jobs.
size_t
active
()
const
;
private:
mutable
std
::
mutex
mutex
;
...
...
@@ -85,54 +53,9 @@ private:
std
::
queue
<
Job
>
jobs
;
std
::
vector
<
std
::
thread
>
threads
;
std
::
vector
<
std
::
exception_ptr
>
exceptions
;
/// NOTE Saving many exceptions but rethrow just first one.
void
worker
()
{
while
(
true
)
{
Job
job
;
{
std
::
unique_lock
<
std
::
mutex
>
lock
(
mutex
);
has_new_job_or_shutdown
.
wait
(
lock
,
[
this
]
{
return
shutdown
||
!
jobs
.
empty
();
});
if
(
!
shutdown
)
{
job
=
std
::
move
(
jobs
.
front
());
jobs
.
pop
();
}
else
{
return
;
}
}
try
{
job
();
}
catch
(...)
{
{
std
::
unique_lock
<
std
::
mutex
>
lock
(
mutex
);
exceptions
.
push_back
(
std
::
current_exception
());
shutdown
=
true
;
--
active_jobs
;
}
has_free_thread
.
notify_one
();
has_new_job_or_shutdown
.
notify_all
();
return
;
}
{
std
::
unique_lock
<
std
::
mutex
>
lock
(
mutex
);
--
active_jobs
;
}
has_free_thread
.
notify_one
();
}
}
std
::
vector
<
std
::
exception_ptr
>
exceptions
;
/// NOTE Saving many exceptions but we rethrow just first one.
void
worker
();
};
dbms/src/Common/ThreadPool.cpp
0 → 100644
浏览文件 @
1bb6ccd7
#include <DB/Common/ThreadPool.h>
ThreadPool
::
ThreadPool
(
size_t
m_size
)
:
m_size
(
m_size
)
{
threads
.
reserve
(
m_size
);
for
(
size_t
i
=
0
;
i
<
m_size
;
++
i
)
threads
.
emplace_back
([
this
]
{
worker
();
});
}
void
ThreadPool
::
schedule
(
Job
job
)
{
{
std
::
unique_lock
<
std
::
mutex
>
lock
(
mutex
);
has_free_thread
.
wait
(
lock
,
[
this
]
{
return
active_jobs
<
m_size
||
shutdown
;
});
if
(
shutdown
)
return
;
jobs
.
push
(
std
::
move
(
job
));
++
active_jobs
;
}
has_new_job_or_shutdown
.
notify_one
();
}
void
ThreadPool
::
wait
()
{
{
std
::
unique_lock
<
std
::
mutex
>
lock
(
mutex
);
has_free_thread
.
wait
(
lock
,
[
this
]
{
return
active_jobs
==
0
||
shutdown
;
});
if
(
!
exceptions
.
empty
())
std
::
rethrow_exception
(
exceptions
.
front
());
}
}
ThreadPool
::~
ThreadPool
()
{
{
std
::
unique_lock
<
std
::
mutex
>
lock
(
mutex
);
shutdown
=
true
;
}
has_new_job_or_shutdown
.
notify_all
();
for
(
auto
&
thread
:
threads
)
thread
.
join
();
}
size_t
ThreadPool
::
active
()
const
{
std
::
unique_lock
<
std
::
mutex
>
lock
(
mutex
);
return
active_jobs
;
}
void
ThreadPool
::
worker
()
{
while
(
true
)
{
Job
job
;
{
std
::
unique_lock
<
std
::
mutex
>
lock
(
mutex
);
has_new_job_or_shutdown
.
wait
(
lock
,
[
this
]
{
return
shutdown
||
!
jobs
.
empty
();
});
if
(
!
shutdown
)
{
job
=
std
::
move
(
jobs
.
front
());
jobs
.
pop
();
}
else
{
return
;
}
}
try
{
job
();
}
catch
(...)
{
{
std
::
unique_lock
<
std
::
mutex
>
lock
(
mutex
);
exceptions
.
push_back
(
std
::
current_exception
());
shutdown
=
true
;
--
active_jobs
;
}
has_free_thread
.
notify_one
();
has_new_job_or_shutdown
.
notify_all
();
return
;
}
{
std
::
unique_lock
<
std
::
mutex
>
lock
(
mutex
);
--
active_jobs
;
}
has_free_thread
.
notify_one
();
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录