diff --git a/src/aws-cpp-sdk-core/include/aws/core/utils/threading/Executor.h b/src/aws-cpp-sdk-core/include/aws/core/utils/threading/Executor.h index 376abf480a0..08ab893080f 100644 --- a/src/aws-cpp-sdk-core/include/aws/core/utils/threading/Executor.h +++ b/src/aws-cpp-sdk-core/include/aws/core/utils/threading/Executor.h @@ -9,7 +9,7 @@ #include #include #include -#include +#include #include #include #include @@ -105,7 +105,7 @@ namespace Aws private: Aws::Queue*> m_tasks; std::mutex m_queueLock; - Aws::Utils::Threading::Semaphore m_sync; + std::condition_variable m_sync; Aws::Vector m_threadTaskHandles; size_t m_poolSize; OverflowPolicy m_overflowPolicy; diff --git a/src/aws-cpp-sdk-core/source/utils/threading/Executor.cpp b/src/aws-cpp-sdk-core/source/utils/threading/Executor.cpp index f9538f00336..2ad8b4dd3e0 100644 --- a/src/aws-cpp-sdk-core/source/utils/threading/Executor.cpp +++ b/src/aws-cpp-sdk-core/source/utils/threading/Executor.cpp @@ -89,12 +89,16 @@ PooledThreadExecutor::PooledThreadExecutor(size_t poolSize, OverflowPolicy overf PooledThreadExecutor::~PooledThreadExecutor() { - for(auto threadTask : m_threadTaskHandles) { - threadTask->StopProcessingWork(); + std::lock_guard lock(m_queueLock); + + for(auto threadTask : m_threadTaskHandles) + { + threadTask->StopProcessingWork(); + } } - m_sync.ReleaseAll(); + m_sync.notify_all(); for (auto threadTask : m_threadTaskHandles) { @@ -131,15 +135,13 @@ bool PooledThreadExecutor::SubmitToThread(std::function&& fn) m_tasks.push(fnCpy); } - m_sync.Release(); + m_sync.notify_one(); return true; } std::function* PooledThreadExecutor::PopTask() { - std::lock_guard locker(m_queueLock); - if (m_tasks.size() > 0) { std::function* fn = m_tasks.front(); @@ -155,6 +157,5 @@ std::function* PooledThreadExecutor::PopTask() bool PooledThreadExecutor::HasTasks() { - std::lock_guard locker(m_queueLock); return m_tasks.size() > 0; } diff --git a/src/aws-cpp-sdk-core/source/utils/threading/ThreadTask.cpp b/src/aws-cpp-sdk-core/source/utils/threading/ThreadTask.cpp index a899fe045d7..76338c9c42b 100644 --- a/src/aws-cpp-sdk-core/source/utils/threading/ThreadTask.cpp +++ b/src/aws-cpp-sdk-core/source/utils/threading/ThreadTask.cpp @@ -21,22 +21,23 @@ ThreadTask::~ThreadTask() void ThreadTask::MainTaskRunner() { + std::unique_lock lock(m_executor.m_queueLock); + while (m_continue) { - while (m_continue && m_executor.HasTasks()) + if (m_executor.HasTasks()) { auto fn = m_executor.PopTask(); + lock.unlock(); if(fn) { (*fn)(); Aws::Delete(fn); } + lock.lock(); } - - if(m_continue) - { - m_executor.m_sync.WaitOne(); - } + + m_executor.m_sync.wait(lock, [this] { return !m_continue || m_executor.HasTasks(); }); } }