Skip to content

Commit 4110537

Browse files
author
Chris Sullivan
committed
Merge pull request #5 from ExtremeScale/master_is_worker
master is worker
2 parents c8d4a76 + eb976d1 commit 4110537

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

include/ThreadPool.hh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public:
4141
m_promises[mypromise].set_value();
4242
});
4343
}
44+
m_taskQueue.pop()(); // master thread is also a worker
4445
Finish();
4546
}
4647
template<typename InputIt, typename T>
@@ -57,9 +58,10 @@ public:
5758
while (threadBegin != threadEnd) {
5859
*(threadOutput++) = func(*(threadBegin++));
5960
}
60-
m_promises[mypromise].set_value();
61+
m_promises[mypromise].set_value(); // master thread is also a worker
6162
});
6263
}
64+
m_taskQueue.pop()();
6365
Finish();
6466
}
6567

src/ThreadPool.cc

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
#include "ThreadPool.hh"
22

33
ThreadPool::ThreadPool(uint32_t numthreads) : m_nthreads(numthreads), m_stopWorkers(false) {
4-
for (uint32_t i=0; i<numthreads;i++) {
5-
m_workers.emplace_back(&ThreadPool::Worker, this);
6-
}
4+
for (uint32_t i=0; i<numthreads-1;i++) { // -1 b/c main thread is also a worker
5+
m_workers.emplace_back(&ThreadPool::Worker, this);
6+
}
77
}
88
ThreadPool::~ThreadPool() {
9-
m_stopWorkers = true;
10-
m_taskQueue.join();
11-
JoinAll();
9+
m_stopWorkers = true;
10+
m_taskQueue.join();
11+
JoinAll();
1212
}
1313

1414
void ThreadPool::Worker() {
@@ -25,7 +25,7 @@ void ThreadPool::Worker() {
2525
}
2626
}
2727
void ThreadPool::JoinAll() {
28-
for (auto& worker : m_workers) { worker.join(); }
28+
for (auto& worker : m_workers) { worker.join(); }
2929
}
3030
void ThreadPool::Finish() {
3131
for (auto& promise : m_promises) promise.get_future().get();

0 commit comments

Comments
 (0)