From 570fe02ca599513cbf817c8aec760ff64dec5f03 Mon Sep 17 00:00:00 2001 From: vng Date: Mon, 4 Mar 2013 21:04:24 +0300 Subject: [PATCH] Add threads::ThreadPool. --- base/thread.cpp | 44 ++++++++++++++++++++++++-- base/thread.hpp | 21 ++++++++++++ map/map_tests/multithread_map_test.cpp | 18 ++--------- 3 files changed, 65 insertions(+), 18 deletions(-) diff --git a/base/thread.cpp b/base/thread.cpp index afc8f8b022..c02e283a73 100644 --- a/base/thread.cpp +++ b/base/thread.cpp @@ -21,6 +21,7 @@ namespace threads /// BADA specific implementation class ThreadImpl : public Osp::Base::Runtime::Thread { + typedef Osp::Base::Runtime::Thread BaseT; IRoutine * m_pRoutine; public: @@ -35,14 +36,13 @@ namespace threads int Create(IRoutine * pRoutine) { - ASSERT(pRoutine, ("Can't be NULL")); m_pRoutine = pRoutine; - return Start(); + return BaseT::Start(); } int Join() { - return Join(); + return BaseT::Join(); } virtual Osp::Base::Object * Run() @@ -179,6 +179,44 @@ namespace threads } } + + ThreadPool::ThreadPool(size_t reserve) + { + m_pool.reserve(reserve); + } + + ThreadPool::~ThreadPool() + { + for (size_t i = 0; i < m_pool.size(); ++i) + { + delete m_pool[i].first; + delete m_pool[i].second; + } + } + + void ThreadPool::Add(IRoutine * pRoutine) + { + ValueT v; + v.first = new Thread(); + v.second = pRoutine; + + m_pool.push_back(v); + + v.first->Create(pRoutine); + } + + void ThreadPool::Join() + { + for (size_t i = 0; i < m_pool.size(); ++i) + m_pool[i].first->Join(); + } + + IRoutine * ThreadPool::GetRoutine(size_t i) const + { + return m_pool[i].second; + } + + void Sleep(size_t ms) { #ifdef OMIM_OS_WINDOWS diff --git a/base/thread.hpp b/base/thread.hpp index 6432205b60..df83e71b31 100644 --- a/base/thread.hpp +++ b/base/thread.hpp @@ -3,6 +3,8 @@ #include "../std/target_os.hpp" #include "../std/stdint.hpp" +#include "../std/vector.hpp" +#include "../std/utility.hpp" #ifdef OMIM_OS_WINDOWS #include "../std/windows.hpp" // for DWORD @@ -53,6 +55,25 @@ namespace threads void Join(); }; + /// Simple threads container. Takes ownership for every added IRoutine. + class ThreadPool + { + typedef pair ValueT; + vector m_pool; + + ThreadPool(ThreadPool const &); + ThreadPool & operator=(Thread const &); + + public: + ThreadPool(size_t reserve = 0); + ~ThreadPool(); + + void Add(IRoutine * pRoutine); + void Join(); + + IRoutine * GetRoutine(size_t i) const; + }; + /// Suspends the execution of the current thread until the time-out interval elapses. /// @param[in] ms time-out interval in milliseconds void Sleep(size_t ms); diff --git a/map/map_tests/multithread_map_test.cpp b/map/map_tests/multithread_map_test.cpp index ff7feb3d4a..ff2df972ab 100644 --- a/map/map_tests/multithread_map_test.cpp +++ b/map/map_tests/multithread_map_test.cpp @@ -74,24 +74,12 @@ namespace srand(666); size_t const count = 20; - vector > pool; - pool.resize(count); + threads::ThreadPool pool(count); for (size_t i = 0; i < count; ++i) - { - pool[i].second = new FeaturesLoader(src); - pool[i].first = new threads::Thread(); + pool.Add(new FeaturesLoader(src)); - pool[i].first->Create(pool[i].second); - } - - for (size_t i = 0; i < count; ++i) - { - pool[i].first->Join(); - - delete pool[i].first; - delete pool[i].second; - } + pool.Join(); } }