Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYuri Gorshenin <ygorshenin@chromium.org>2015-03-13 17:55:10 +0300
committerAlex Zolotarev <alex@maps.me>2015-09-23 02:41:24 +0300
commit2b0f4125289657beaa71b80f5897052d36521104 (patch)
tree31a656501d5afcc9d296c878ebed88aa9b71ce1f /base/thread.cpp
parentf7203eb9b75a01270d63e74e8eccebdf531c5742 (diff)
[thread] Refactored lifetime management of multithreaded tasks.
Diffstat (limited to 'base/thread.cpp')
-rw-r--r--base/thread.cpp40
1 files changed, 14 insertions, 26 deletions
diff --git a/base/thread.cpp b/base/thread.cpp
index f63d0ca068..111ae7ca73 100644
--- a/base/thread.cpp
+++ b/base/thread.cpp
@@ -1,5 +1,4 @@
#include "thread.hpp"
-#include "assert.hpp"
#include "../base/logging.hpp"
@@ -16,7 +15,7 @@ namespace threads
namespace
{
/// Prepares worker thread and runs routine.
-void RunRoutine(IRoutine * routine)
+void RunRoutine(shared_ptr<IRoutine> routine)
{
#if defined(OMIM_OS_ANDROID)
AndroidThreadAttachToJVM();
@@ -32,7 +31,6 @@ void RunRoutine(IRoutine * routine)
/////////////////////////////////////////////////////////////////////
// Thread wrapper implementation
-Thread::Thread() : m_routine(0) {}
Thread::~Thread()
{
@@ -45,21 +43,22 @@ Thread::~Thread()
m_thread.detach();
}
-bool Thread::Create(IRoutine * pRoutine)
+bool Thread::Create(unique_ptr<IRoutine> && routine)
{
ASSERT(!m_routine, ("Current implementation doesn't allow to reuse thread"));
thread routineThread;
try
{
- routineThread = thread(&RunRoutine, pRoutine);
+ m_routine.reset(routine.release());
+ routineThread = thread(&RunRoutine, m_routine);
}
catch (exception & e)
{
LOG(LERROR, ("Thread creation failed with error:", e.what()));
+ m_routine.reset();
return false;
}
m_thread = move(routineThread);
- m_routine = pRoutine;
return true;
}
@@ -69,6 +68,7 @@ void Thread::Cancel()
return;
m_routine->Cancel();
Join();
+ m_routine.reset();
}
void Thread::Join()
@@ -77,35 +77,23 @@ void Thread::Join()
m_thread.join();
}
-SimpleThreadPool::SimpleThreadPool(size_t reserve) { m_pool.reserve(reserve); }
+IRoutine * Thread::GetRoutine() { return m_routine.get(); }
-SimpleThreadPool::~SimpleThreadPool()
-{
- for (size_t i = 0; i < m_pool.size(); ++i)
- {
- delete m_pool[i].first;
- delete m_pool[i].second;
- }
-}
+SimpleThreadPool::SimpleThreadPool(size_t reserve) { m_pool.reserve(reserve); }
-void SimpleThreadPool::Add(IRoutine * pRoutine)
+void SimpleThreadPool::Add(unique_ptr<IRoutine> && routine)
{
- ValueT v;
- v.first = new Thread();
- v.second = pRoutine;
-
- m_pool.push_back(v);
-
- v.first->Create(pRoutine);
+ m_pool.emplace_back(new Thread());
+ m_pool.back()->Create(move(routine));
}
void SimpleThreadPool::Join()
{
- for (size_t i = 0; i < m_pool.size(); ++i)
- m_pool[i].first->Join();
+ for (auto & thread : m_pool)
+ thread->Join();
}
-IRoutine * SimpleThreadPool::GetRoutine(size_t i) const { return m_pool[i].second; }
+IRoutine * SimpleThreadPool::GetRoutine(size_t i) const { return m_pool[i]->GetRoutine(); }
void Sleep(size_t ms) { this_thread::sleep_for(milliseconds(ms)); }