Skip to content

Commit 6842142

Browse files
committed
Fixed race condition in threading
Fixed exceeding worker count limit if all current workers are exiting
1 parent 1c3b2a7 commit 6842142

1 file changed

Lines changed: 6 additions & 3 deletions

File tree

src/threading.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,11 @@ std::future<bool> Threading::pushTask(mariadb::connection_ref con, std::function
168168
auto found = workers.find(con->account());
169169
if (found != workers.end()) {
170170

171-
if (found->second.size() == Config::get().getWorkerCount()) {
171+
auto activeWorkerCount = std::count_if(found->second.begin(), found->second.end(), [](const std::shared_ptr<Worker>& w) {
172+
return !w->exiting;
173+
});
174+
175+
if (activeWorkerCount >= Config::get().getWorkerCount()) {
172176
const auto it = std::min_element(found->second.begin(), found->second.end(), [](const std::shared_ptr<Worker>& l, const std::shared_ptr<Worker>& r) {
173177
if (l->exiting) return false;
174178
if (r->exiting) return true;
@@ -257,8 +261,7 @@ bool Threading::isConnected(mariadb::account_ref acc) {
257261
}
258262

259263
void Threading::pushAsyncWork(ref<GameDataDBAsyncResult> work) {
260-
//std::unique_lock l(asyncWorkMutex);
261-
//Only called from main thread
264+
std::unique_lock l(asyncWorkMutex);
262265
asyncWork.emplace_back(work);
263266
}
264267
__itt_string_handle* threading_updateAsyncWorkLists = __itt_string_handle_create("Threading::updateAsyncWorkLists");

0 commit comments

Comments
 (0)