Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions CometSearch/ThreadPool.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#include "BS_thread_pool.hpp"
#include <functional>
#include <stdexcept>
#include <chrono>
#include <thread>

#ifdef _WIN32
#include <windows.h>
Expand Down Expand Up @@ -67,13 +69,36 @@ class ThreadPool
if (pool_)
{
// Wait until at least one thread is available
// Use progressive backoff to reduce CPU usage:
// 1. Yield a few times (low latency, low cost)
// 2. Then sleep with increasing duration (reduces CPU burn)
static constexpr int MAX_YIELD_ATTEMPTS = 10;
static constexpr int MAX_SHORT_SLEEP_ATTEMPTS = 20;
static constexpr int MAX_BACKOFF_LEVEL = MAX_YIELD_ATTEMPTS + MAX_SHORT_SLEEP_ATTEMPTS;
int attempts = 0;

while (pool_->get_tasks_running() >= pool_->get_thread_count())
{
if (attempts < MAX_YIELD_ATTEMPTS)
{
// Fast path: just yield to other threads
#ifdef _WIN32
SwitchToThread();
SwitchToThread();
#else
sched_yield();
sched_yield();
#endif
}
else if (attempts < MAX_BACKOFF_LEVEL)
{
// Medium path: short sleep (1ms)
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
else
{
// Long wait path: longer sleep (5ms) to avoid burning CPU
std::this_thread::sleep_for(std::chrono::milliseconds(5));
}
attempts++;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion MSToolkit/extern/expat-2.2.9/expat_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
/* #undef HAVE_ARC4RANDOM */

/* Define to 1 if you have the `arc4random_buf' function. */
/* #undef HAVE_ARC4RANDOM_BUF */
#define HAVE_ARC4RANDOM_BUF 1

/* Define to 1 if you have the <dlfcn.h> header file. */
#define HAVE_DLFCN_H 1
Expand Down
Loading