Skip to content

Add progressive backoff to wait_for_available_thread()#107

Merged
jke000 merged 11 commits intoBS_thread_poolfrom
copilot/sub-pr-102-another-one
Feb 11, 2026
Merged

Add progressive backoff to wait_for_available_thread()#107
jke000 merged 11 commits intoBS_thread_poolfrom
copilot/sub-pr-102-another-one

Conversation

Copy link
Contributor

Copilot AI commented Feb 11, 2026

The wait_for_available_thread() function was a tight busy-wait loop that continuously yielded, burning significant CPU when tasks are long-lived or the system is oversubscribed.

Changes

  • Implemented progressive backoff strategy:
    • Yields 10 times (low latency fast path)
    • Sleeps 1ms for next 20 attempts
    • Sleeps 5ms thereafter
void wait_for_available_thread()
{
   if (pool_)
   {
      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: yield to other threads
            sched_yield();  // or SwitchToThread() on Windows
         }
         else if (attempts < MAX_BACKOFF_LEVEL)
         {
            std::this_thread::sleep_for(std::chrono::milliseconds(1));
         }
         else
         {
            std::this_thread::sleep_for(std::chrono::milliseconds(5));
         }
         attempts++;
      }
   }
}

This preserves low latency for short waits while dramatically reducing CPU usage under sustained load.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 10 commits February 11, 2026 01:41
Co-authored-by: jke000 <9449681+jke000@users.noreply.github.com>
Co-authored-by: jke000 <9449681+jke000@users.noreply.github.com>
…heck

Co-authored-by: jke000 <9449681+jke000@users.noreply.github.com>
Co-authored-by: jke000 <9449681+jke000@users.noreply.github.com>
Co-authored-by: jke000 <9449681+jke000@users.noreply.github.com>
Co-authored-by: jke000 <9449681+jke000@users.noreply.github.com>
Co-authored-by: jke000 <9449681+jke000@users.noreply.github.com>
Co-authored-by: jke000 <9449681+jke000@users.noreply.github.com>
Co-authored-by: jke000 <9449681+jke000@users.noreply.github.com>
Co-authored-by: jke000 <9449681+jke000@users.noreply.github.com>
Copilot AI changed the title [WIP] Apply changes based on feedback for BS::thread_pool PR Add progressive backoff to wait_for_available_thread() Feb 11, 2026
Copilot AI requested a review from jke000 February 11, 2026 01:55
@jke000 jke000 marked this pull request as ready for review February 11, 2026 02:09
@jke000 jke000 merged commit 1275648 into BS_thread_pool Feb 11, 2026
@jke000 jke000 deleted the copilot/sub-pr-102-another-one branch February 17, 2026 21:53
jke000 added a commit that referenced this pull request Feb 17, 2026
* Update threading code to use BS::thread_pool.

* update Makefiles to use C++ 20

* fix typos in LICENSE

* remove NOTICE file; update windows-build.yml to use VS2022 to compile; map negative value to single worker thread

* Throw exception when ThreadPool::doJob() called before initialization (#103)

* Initial plan

* Add error handling to doJob() to prevent silent work dropping

Co-authored-by: jke000 <9449681+jke000@users.noreply.github.com>

* Complete implementation of doJob() error handling

Co-authored-by: jke000 <9449681+jke000@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: jke000 <9449681+jke000@users.noreply.github.com>

* Remove unused thread tracking members from Threading class (#104)

* Initial plan

* Remove unused _threadMapMutex and _threads members

Co-authored-by: jke000 <9449681+jke000@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: jke000 <9449681+jke000@users.noreply.github.com>

* Update MSToolkit/VisualStudio/extern/zlibstat.vcxproj

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update CometSearch/Threading.cpp

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update CometSearch/ThreadPool.h

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Initial plan (#105)

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>

* Remove unused EndThread() function from Threading interface (#106)

* Initial plan

* Remove unused EndThread() function from Threading interface

Co-authored-by: jke000 <9449681+jke000@users.noreply.github.com>

* Remove build artifacts that were accidentally committed

Co-authored-by: jke000 <9449681+jke000@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: jke000 <9449681+jke000@users.noreply.github.com>

* Add progressive backoff to wait_for_available_thread() (#107)

* Initial plan

* Add progressive backoff to wait_for_available_thread to reduce CPU usage

Co-authored-by: jke000 <9449681+jke000@users.noreply.github.com>

* Cap attempts counter to prevent overflow in wait_for_available_thread

Co-authored-by: jke000 <9449681+jke000@users.noreply.github.com>

* Simplify wait_for_available_thread by removing unnecessary overflow check

Co-authored-by: jke000 <9449681+jke000@users.noreply.github.com>

* Add counter cap to prevent overflow in wait_for_available_thread

Co-authored-by: jke000 <9449681+jke000@users.noreply.github.com>

* Clarify comment for counter cap in wait_for_available_thread

Co-authored-by: jke000 <9449681+jke000@users.noreply.github.com>

* Use std::min for cleaner counter cap in wait_for_available_thread

Co-authored-by: jke000 <9449681+jke000@users.noreply.github.com>

* Simplify counter logic by removing unnecessary cap

Co-authored-by: jke000 <9449681+jke000@users.noreply.github.com>

* Make backoff constants static constexpr for clarity

Co-authored-by: jke000 <9449681+jke000@users.noreply.github.com>

* Final progress update

Co-authored-by: jke000 <9449681+jke000@users.noreply.github.com>

* Remove build artifacts that shouldn't be committed

Co-authored-by: jke000 <9449681+jke000@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: jke000 <9449681+jke000@users.noreply.github.com>

* threadpool update; confirmed working for both VS and Linux

* remove fflush(); let the system handle buffering and flushing when writing the output files

* get rid of _codeql_detected_source_root and add it to .gitignore

---------

Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com>
Co-authored-by: jke000 <9449681+jke000@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants