forked from organicmaps/organicmaps
Add threads::ThreadPool.
This commit is contained in:
parent
987f5932ae
commit
570fe02ca5
3 changed files with 65 additions and 18 deletions
|
@ -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
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -74,24 +74,12 @@ namespace
|
|||
srand(666);
|
||||
|
||||
size_t const count = 20;
|
||||
vector<pair<threads::Thread *, FeaturesLoader *> > 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();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue