diff options
Diffstat (limited to 'fastos')
-rw-r--r-- | fastos/src/tests/job.h | 2 | ||||
-rw-r--r-- | fastos/src/tests/thread_test_base.hpp | 6 | ||||
-rw-r--r-- | fastos/src/vespa/fastos/thread.h | 12 |
3 files changed, 13 insertions, 7 deletions
diff --git a/fastos/src/tests/job.h b/fastos/src/tests/job.h index 15356270972..4546cfe1daa 100644 --- a/fastos/src/tests/job.h +++ b/fastos/src/tests/job.h @@ -28,7 +28,7 @@ public: std::mutex *mutex; std::condition_variable *condition; FastOS_ThreadInterface *otherThread, *ownThread; - int result; + std::atomic<int> result; FastOS_ThreadId _threadId; Job() diff --git a/fastos/src/tests/thread_test_base.hpp b/fastos/src/tests/thread_test_base.hpp index 49d37209b6a..eb994537f6e 100644 --- a/fastos/src/tests/thread_test_base.hpp +++ b/fastos/src/tests/thread_test_base.hpp @@ -5,7 +5,7 @@ #include <chrono> #include <thread> -static volatile int64_t number; +static std::atomic<int64_t> number; #define INCREASE_NUMBER_AMOUNT 10000 using namespace std::chrono_literals; @@ -97,11 +97,11 @@ void ThreadTestBase::Run (FastOS_ThreadInterface *thread, void *arg) guard = std::unique_lock<std::mutex>(*job->mutex); } - result = static_cast<int>(number); + result = static_cast<int>(number.load(std::memory_order_relaxed)); int sleepOn = (INCREASE_NUMBER_AMOUNT/2) * 321/10000; for (int i=0; i<(INCREASE_NUMBER_AMOUNT/2); i++) { - number = number + 2; + number.fetch_add(2, std::memory_order_relaxed); if (i == sleepOn) std::this_thread::sleep_for(1ms); diff --git a/fastos/src/vespa/fastos/thread.h b/fastos/src/vespa/fastos/thread.h index 0f4cc4c09f7..f881095c29a 100644 --- a/fastos/src/vespa/fastos/thread.h +++ b/fastos/src/vespa/fastos/thread.h @@ -50,7 +50,7 @@ private: bool _closeCalledFlag; // Always lock in this order - std::mutex _freeMutex; + mutable std::mutex _freeMutex; std::mutex _liveMutex; std::condition_variable _liveCond; /** @@ -191,7 +191,10 @@ public: * @ref GetNumActiveThreads() and @ref GetNumInactiveThreads(). * @return Number of currently active threads */ - int GetNumActiveThreads () const { return _numActive; } + int GetNumActiveThreads () const { + std::lock_guard<std::mutex> guard(_freeMutex); + return _numActive; + } /** * Get the number of currently inactive threads. @@ -199,7 +202,10 @@ public: * @ref GetNumActiveThreads() and @ref GetNumInactiveThreads(). * @return Number of currently inactive threads */ - int GetNumInactiveThreads () const { return _numFree; } + int GetNumInactiveThreads () const { + std::lock_guard<std::mutex> guard(_freeMutex); + return _numFree; + } /** * Get the number of started threads since instantiation of the thread pool. |