Skip to content

Commit f8983be

Browse files
author
Joshua Bradt
committed
Merge branch 'ntasks' into 'master'.
- Removes master as worker problem - Resolves #6
2 parents 4110537 + e0867e7 commit f8983be

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

include/ThreadPool.hh

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,23 @@ public:
2626
void Finish();
2727

2828
template <typename T, typename... Params>
29-
void ParallelFor(uint32_t begin, uint32_t end, T SerialFunction, Params&&... params) {
29+
void ParallelFor(uint32_t begin, uint32_t end, int32_t n_tasks, T SerialFunction, Params&&... params) {
3030

31-
int chunk = (end - begin) / m_nthreads;
32-
for (int i = 0; i < m_nthreads; ++i) {
31+
n_tasks = (n_tasks >= m_nthreads) ? n_tasks : m_nthreads;
32+
int chunk = (end - begin) / n_tasks;
33+
for (int i = 0; i < n_tasks; ++i) {
3334
m_promises.emplace_back();
3435
int mypromise = m_promises.size() - 1;
3536
m_taskQueue.push([=]{
3637
uint32_t threadstart = begin + i*chunk;
37-
uint32_t threadstop = (i == m_nthreads - 1) ? end : threadstart + chunk;
38+
uint32_t threadstop = (i == n_tasks - 1) ? end : threadstart + chunk;
3839
for (uint32_t it = threadstart; it < threadstop; ++it) {
3940
SerialFunction(it, params...);
4041
}
4142
m_promises[mypromise].set_value();
4243
});
4344
}
44-
m_taskQueue.pop()(); // master thread is also a worker
45+
//m_taskQueue.pop()(); // master thread is also a worker
4546
Finish();
4647
}
4748
template<typename InputIt, typename T>
@@ -61,7 +62,7 @@ public:
6162
m_promises[mypromise].set_value(); // master thread is also a worker
6263
});
6364
}
64-
m_taskQueue.pop()();
65+
//m_taskQueue.pop()();
6566
Finish();
6667
}
6768

src/ThreadPool.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
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-1;i++) { // -1 b/c main thread is also a worker
4+
//for (uint32_t i=0; i<numthreads-1;i++) { // -1 b/c main thread is also a worker
5+
for (uint32_t i=0; i<numthreads;i++) {
56
m_workers.emplace_back(&ThreadPool::Worker, this);
67
}
78
}

0 commit comments

Comments
 (0)