From c9cb50f4ddbd208dddd2cc73f1a8b3133d75b207 Mon Sep 17 00:00:00 2001 From: DanS Date: Fri, 9 Oct 2026 21:09:48 -0500 Subject: [PATCH] fix(mining): make XmrigManager's thread count atomic The requested thread count is written by generateConfig() on the mining-control thread and read on the UI thread (the thread-grid sync under idle thread scaling, the pool auto-balance restart) without any synchronisation. The write also stored the unclamped count first and clamped it afterwards, so a reader could see more threads than cores. It is now an atomic, set by one store of the clamped value. Co-Authored-By: Claude Opus 5.5 --- src/daemon/xmrig_manager.cpp | 9 +++++---- src/daemon/xmrig_manager.h | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/daemon/xmrig_manager.cpp b/src/daemon/xmrig_manager.cpp index 776896b..03ab12d 100644 --- a/src/daemon/xmrig_manager.cpp +++ b/src/daemon/xmrig_manager.cpp @@ -173,9 +173,10 @@ bool XmrigManager::generateConfig(const Config& cfg, const std::string& outPath) int hw = (int)std::thread::hardware_concurrency(); if (hw < 1) hw = 1; - // Use explicit thread count (not just a hint) - threads_ = (cfg.threads > 0) ? cfg.threads : std::max(1, hw / 2); - if (threads_ > hw) threads_ = hw; + // Use explicit thread count (not just a hint). One store: the UI thread reads it (getRequestedThreads). + int t = (cfg.threads > 0) ? cfg.threads : std::max(1, hw / 2); + if (t > hw) t = hw; + threads_ = t; json j; j["autosave"] = false; @@ -324,7 +325,7 @@ bool XmrigManager::start(const Config& cfg) { return false; } DEBUG_LOGF("[INFO] XmrigManager: config written to %s (API port %d, threads %d)\n", - cfgPath.c_str(), api_port_, threads_); + cfgPath.c_str(), api_port_, threads_.load()); // Spawn process if (!startProcess(binary, cfgPath, threads_)) { diff --git a/src/daemon/xmrig_manager.h b/src/daemon/xmrig_manager.h index 54d1834..3a7bf8d 100644 --- a/src/daemon/xmrig_manager.h +++ b/src/daemon/xmrig_manager.h @@ -173,7 +173,7 @@ private: // xmrig HTTP API credentials (random per session) int api_port_ = 0; std::string api_token_; - int threads_ = 0; // Thread count for mining + std::atomic threads_{0}; // Thread count for mining (written on the mining-control thread, read by the UI) std::string pool_host_; // Pool hostname for stats API PoolStats stats_; mutable std::mutex stats_mutex_;