From 4999aef71e620a0684e447589e04865c29b51d51 Mon Sep 17 00:00:00 2001 From: Stephen Webb Date: Thu, 17 Oct 2024 12:39:03 +1100 Subject: [PATCH] Help the scheduler when there are more threads than physical cores --- src/main/cpp/asyncappender.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/main/cpp/asyncappender.cpp b/src/main/cpp/asyncappender.cpp index 565a683c3..4205e5570 100644 --- a/src/main/cpp/asyncappender.cpp +++ b/src/main/cpp/asyncappender.cpp @@ -310,10 +310,13 @@ void AsyncAppender::append(const spi::LoggingEventPtr& event, Pool& p) // Write to the ring buffer priv->buffer[index] = AsyncAppenderPriv::EventData{event, pendingCount}; // Notify the dispatch thread that an event has been added + auto failureCount = 0; auto savedEventCount = oldEventCount; - while (!priv->commitCount.compare_exchange_weak(oldEventCount, oldEventCount + 1, std::memory_order_acq_rel)) + while (!priv->commitCount.compare_exchange_weak(oldEventCount, oldEventCount + 1, std::memory_order_release)) { - oldEventCount = savedEventCount; + oldEventCount = savedEventCount; + if (2 < ++failureCount) // Did the scheduler suspend a thread between claiming a slot and advancing commitCount? + std::this_thread::yield(); // Wait a bit } priv->bufferNotEmpty.notify_all(); break;