Skip to content

Commit 4cefdf6

Browse files
author
Chris Sullivan
committed
Added n_tasks parameter to LockfreeTP::ParallelFor
1 parent ab0c31f commit 4cefdf6

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

include/LockfreeThreadPool.hh

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,16 @@ public:
2727
void Finish();
2828

2929
template <typename T, typename... Params>
30-
void ParallelFor(uint32_t begin, uint32_t end, T SerialFunction, Params&&... params) {
30+
void ParallelFor(uint32_t begin, uint32_t end, int32_t n_tasks, T SerialFunction, Params&&... params) {
3131

32-
int chunk = (end - begin) / m_nthreads;
33-
for (int i = 0; i < m_nthreads; ++i) {
32+
n_tasks = (n_tasks >= m_nthreads) ? n_tasks : m_nthreads;
33+
int chunk = (end - begin) / n_tasks;
34+
for (int i = 0; i < n_tasks; ++i) {
3435
m_promises.emplace_back();
3536
int mypromise = m_promises.size() - 1;
3637
m_taskQueue.enqueue([=]{
3738
uint32_t threadstart = begin + i*chunk;
38-
uint32_t threadstop = (i == m_nthreads - 1) ? end : threadstart + chunk;
39+
uint32_t threadstop = (i == n_tasks - 1) ? end : threadstart + chunk;
3940
for (uint32_t it = threadstart; it < threadstop; ++it) {
4041
SerialFunction(it, params...);
4142
}

0 commit comments

Comments
 (0)