diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2022-02-21 17:50:17 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-02-21 17:50:17 +0100 |
commit | a7e8bb9dcf3c674a3756e0f0383384593856415a (patch) | |
tree | 3944389e6b3d0e5b0ef7992808a3ca1ff24ff260 /vespalib | |
parent | f67ad6e4bdb5cf4b834428c61bc18953d9efd761 (diff) | |
parent | eddc91fb205d4bc8e68aa72be86ed39a199728b5 (diff) |
Merge pull request #21285 from vespa-engine/vekterli/more-threading-fixes
More miscellaneous threading fixes [run-systemtest]
Diffstat (limited to 'vespalib')
-rw-r--r-- | vespalib/src/vespa/vespalib/util/thread.cpp | 8 | ||||
-rw-r--r-- | vespalib/src/vespa/vespalib/util/thread.h | 7 |
2 files changed, 9 insertions, 6 deletions
diff --git a/vespalib/src/vespa/vespalib/util/thread.cpp b/vespalib/src/vespa/vespalib/util/thread.cpp index c3230bf313d..ffa9f385967 100644 --- a/vespalib/src/vespa/vespalib/util/thread.cpp +++ b/vespalib/src/vespa/vespalib/util/thread.cpp @@ -60,7 +60,7 @@ Thread & Thread::stop() { std::unique_lock guard(_lock); - _stopped = true; + _stopped.store(true, std::memory_order_relaxed); _cond.notify_all(); return *this; } @@ -75,14 +75,14 @@ bool Thread::slumber(double s) { std::unique_lock guard(_lock); - if (!_stopped || _woken) { + if (!stopped() || _woken) { if (_cond.wait_for(guard, from_s(s)) == std::cv_status::no_timeout) { - _woken = _stopped; + _woken = stopped(); } } else { _woken = true; } - return !_stopped; + return !stopped(); } Thread & diff --git a/vespalib/src/vespa/vespalib/util/thread.h b/vespalib/src/vespa/vespalib/util/thread.h index e08f3ca1100..0c7693556c7 100644 --- a/vespalib/src/vespa/vespalib/util/thread.h +++ b/vespalib/src/vespa/vespalib/util/thread.h @@ -6,6 +6,7 @@ #include "runnable.h" #include "active.h" #include <vespa/fastos/thread.h> +#include <atomic> namespace vespalib { @@ -37,7 +38,7 @@ private: FastOS_ThreadPool _pool; std::mutex _lock; std::condition_variable _cond; - bool _stopped; + std::atomic<bool> _stopped; bool _woken; public: @@ -46,7 +47,9 @@ public: void start() override; Thread &stop() override; void join() override; - bool stopped() const { return _stopped; } + [[nodiscard]] bool stopped() const noexcept { + return _stopped.load(std::memory_order_relaxed); + } bool slumber(double s); static Thread ¤tThread(); static void sleep(size_t ms); |