diff options
author | vng <viktor.govako@gmail.com> | 2013-03-04 22:04:24 +0400 |
---|---|---|
committer | Alex Zolotarev <alex@maps.me> | 2015-09-23 01:51:34 +0300 |
commit | 570fe02ca599513cbf817c8aec760ff64dec5f03 (patch) | |
tree | 7106ba291f1bb2a29084ec63d4b7c9a921898fe1 /base | |
parent | 987f5932aeb79f2700789497e1fc34c62915828f (diff) |
Add threads::ThreadPool.
Diffstat (limited to 'base')
-rw-r--r-- | base/thread.cpp | 44 | ||||
-rw-r--r-- | base/thread.hpp | 21 |
2 files changed, 62 insertions, 3 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<Thread *, IRoutine *> ValueT; + vector<ValueT> 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); |