Skip to content

Commit

Permalink
Fixed thread load balancing issue
Browse files Browse the repository at this point in the history
  • Loading branch information
sumeetchhetri committed Aug 9, 2019
1 parent 2c44700 commit fd69b7c
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions src/modules/threads/ThreadPool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,17 @@ void ThreadPool::joinAll() {
}

void ThreadPool::submit(Task* task) {
if(currentThread==(int)tpool.size()) {
currentThread = 0;
}
tpool.at(currentThread++)->addTask(task);
//https://stackoverflow.com/questions/33554255/c-thread-safe-increment-with-modulo-without-mutex-using-stdatomic
int index = currentThread ++;
int id = index % maxThreads;
// If size could wrap, then re-write the modulo value.
// oldValue keeps getting re-read.
// modulo occurs when nothing else updates it.
int oldValue = currentThread;
int newValue = oldValue % maxThreads;
while (!currentThread.compare_exchange_weak( oldValue, newValue, std::memory_order_relaxed ))
newValue = oldValue % maxThreads;
tpool.at(id)->addTask(task);
}
void ThreadPool::submit(Task* task, const int& priority) {
if(this->prioritypooling) {
Expand All @@ -144,7 +151,17 @@ void ThreadPool::schedule(Task* task, const long long& tunit, const int& type) {
}

void ThreadPool::submit(FutureTask* task) {
tpool.at(currentThread)->addTask(task);
//https://stackoverflow.com/questions/33554255/c-thread-safe-increment-with-modulo-without-mutex-using-stdatomic
int index = currentThread ++;
int id = index % maxThreads;
// If size could wrap, then re-write the modulo value.
// oldValue keeps getting re-read.
// modulo occurs when nothing else updates it.
int oldValue = currentThread;
int newValue = oldValue % maxThreads;
while (!currentThread.compare_exchange_weak( oldValue, newValue, std::memory_order_relaxed ))
newValue = oldValue % maxThreads;
tpool.at(id)->addTask(task);
}
void ThreadPool::submit(FutureTask* task, const int& priority) {
if(this->prioritypooling) {
Expand Down

0 comments on commit fd69b7c

Please sign in to comment.