Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
opendht
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Model registry
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
savoirfairelinux
opendht
Commits
bc32f848
Commit
bc32f848
authored
3 years ago
by
Adrien Béraud
Browse files
Options
Downloads
Patches
Plain Diff
thread pool: cleanup
parent
edee6d63
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
include/opendht/thread_pool.h
+4
-5
4 additions, 5 deletions
include/opendht/thread_pool.h
src/thread_pool.cpp
+11
-24
11 additions, 24 deletions
src/thread_pool.cpp
tests/threadpooltester.cpp
+1
-1
1 addition, 1 deletion
tests/threadpooltester.cpp
with
16 additions
and
30 deletions
include/opendht/thread_pool.h
+
4
−
5
View file @
bc32f848
...
...
@@ -65,15 +65,14 @@ public:
void
join
();
private
:
struct
ThreadState
;
std
::
queue
<
std
::
function
<
void
()
>>
tasks_
{};
std
::
vector
<
std
::
unique_ptr
<
ThreadState
>>
threads_
;
unsigned
readyThreads_
{
0
};
std
::
mutex
lock_
{};
std
::
condition_variable
cv_
{};
std
::
queue
<
std
::
function
<
void
()
>>
tasks_
{};
std
::
vector
<
std
::
unique_ptr
<
std
::
thread
>>
threads_
;
unsigned
readyThreads_
{
0
};
bool
running_
{
true
};
const
unsigned
maxThreads_
;
bool
running_
{
true
};
};
class
OPENDHT_PUBLIC
Executor
:
public
std
::
enable_shared_from_this
<
Executor
>
{
...
...
This diff is collapsed.
Click to expand it.
src/thread_pool.cpp
+
11
−
24
View file @
bc32f848
...
...
@@ -28,12 +28,6 @@ namespace dht {
constexpr
const
size_t
IO_THREADS_MAX
{
64
};
struct
ThreadPool
::
ThreadState
{
std
::
thread
thread
{};
std
::
atomic_bool
run
{
true
};
};
ThreadPool
&
ThreadPool
::
computation
()
{
...
...
@@ -67,14 +61,12 @@ void
ThreadPool
::
run
(
std
::
function
<
void
()
>&&
cb
)
{
std
::
unique_lock
<
std
::
mutex
>
l
(
lock_
);
if
(
not
running_
)
return
;
if
(
not
cb
or
not
running_
)
return
;
// launch new thread if necessary
if
(
not
readyThreads_
&&
threads_
.
size
()
<
maxThreads_
)
{
threads_
.
emplace_back
(
new
ThreadState
());
auto
&
t
=
*
threads_
.
back
();
t
.
thread
=
std
::
thread
([
&
]()
{
while
(
t
.
run
)
{
threads_
.
emplace_back
(
std
::
make_unique
<
std
::
thread
>
([
this
]()
{
while
(
true
)
{
std
::
function
<
void
()
>
task
;
// pick task from queue
...
...
@@ -82,10 +74,10 @@ ThreadPool::run(std::function<void()>&& cb)
std
::
unique_lock
<
std
::
mutex
>
l
(
lock_
);
readyThreads_
++
;
cv_
.
wait
(
l
,
[
&
](){
return
not
t
.
run
or
not
tasks_
.
empty
();
return
not
run
ning_
or
not
tasks_
.
empty
();
});
readyThreads_
--
;
if
(
not
t
.
run
)
if
(
not
run
ning_
)
break
;
task
=
std
::
move
(
tasks_
.
front
());
tasks_
.
pop
();
...
...
@@ -93,14 +85,13 @@ ThreadPool::run(std::function<void()>&& cb)
// run task
try
{
if
(
task
)
task
();
task
();
}
catch
(
const
std
::
exception
&
e
)
{
// LOG_ERR("Exception running task: %s", e.what());
std
::
cerr
<<
"Exception running task: "
<<
e
.
what
()
<<
std
::
endl
;
}
}
});
})
)
;
}
// push task to queue
...
...
@@ -113,12 +104,8 @@ ThreadPool::run(std::function<void()>&& cb)
void
ThreadPool
::
stop
()
{
{
std
::
lock_guard
<
std
::
mutex
>
l
(
lock_
);
running_
=
false
;
}
for
(
auto
&
t
:
threads_
)
t
->
run
=
false
;
std
::
lock_guard
<
std
::
mutex
>
l
(
lock_
);
running_
=
false
;
cv_
.
notify_all
();
}
...
...
@@ -127,7 +114,7 @@ ThreadPool::join()
{
stop
();
for
(
auto
&
t
:
threads_
)
t
->
thread
.
join
();
t
->
join
();
threads_
.
clear
();
}
...
...
@@ -147,7 +134,7 @@ Executor::run_(std::function<void()>&& task)
{
current_
++
;
std
::
weak_ptr
<
Executor
>
w
=
shared_from_this
();
threadPool_
.
get
().
run
([
w
,
task
]
{
threadPool_
.
get
().
run
([
w
,
task
=
std
::
move
(
task
)
]
{
try
{
task
();
}
catch
(
const
std
::
exception
&
e
)
{
...
...
This diff is collapsed.
Click to expand it.
tests/threadpooltester.cpp
+
1
−
1
View file @
bc32f848
...
...
@@ -48,7 +48,7 @@ ThreadPoolTester::testThreadPool() {
std
::
this_thread
::
sleep_for
(
std
::
chrono
::
milliseconds
(
10
));
pool
.
join
();
CPPUNIT_ASSERT
(
count
.
load
()
==
N
);
CPPUNIT_ASSERT
_EQUAL
(
N
,
count
.
load
());
}
void
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment