File tree Expand file tree Collapse file tree 2 files changed +10
-8
lines changed Expand file tree Collapse file tree 2 files changed +10
-8
lines changed Original file line number Diff line number Diff line change @@ -41,6 +41,7 @@ public:
41
41
m_promises[mypromise].set_value ();
42
42
});
43
43
}
44
+ m_taskQueue.pop ()(); // master thread is also a worker
44
45
Finish ();
45
46
}
46
47
template <typename InputIt, typename T>
@@ -57,9 +58,10 @@ public:
57
58
while (threadBegin != threadEnd) {
58
59
*(threadOutput++) = func (*(threadBegin++));
59
60
}
60
- m_promises[mypromise].set_value ();
61
+ m_promises[mypromise].set_value (); // master thread is also a worker
61
62
});
62
63
}
64
+ m_taskQueue.pop ()();
63
65
Finish ();
64
66
}
65
67
Original file line number Diff line number Diff line change 1
1
#include " ThreadPool.hh"
2
2
3
3
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
+ }
7
7
}
8
8
ThreadPool::~ThreadPool () {
9
- m_stopWorkers = true ;
10
- m_taskQueue.join ();
11
- JoinAll ();
9
+ m_stopWorkers = true ;
10
+ m_taskQueue.join ();
11
+ JoinAll ();
12
12
}
13
13
14
14
void ThreadPool::Worker () {
@@ -25,7 +25,7 @@ void ThreadPool::Worker() {
25
25
}
26
26
}
27
27
void ThreadPool::JoinAll () {
28
- for (auto & worker : m_workers) { worker.join (); }
28
+ for (auto & worker : m_workers) { worker.join (); }
29
29
}
30
30
void ThreadPool::Finish () {
31
31
for (auto & promise : m_promises) promise.get_future ().get ();
You can’t perform that action at this time.
0 commit comments