summaryrefslogtreecommitdiffstats
path: root/src/Net/ThreadManager.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/Net/ThreadManager.cpp')
-rw-r--r--src/Net/ThreadManager.cpp31
1 files changed, 15 insertions, 16 deletions
diff --git a/src/Net/ThreadManager.cpp b/src/Net/ThreadManager.cpp
index d24ba01..377e6e1 100644
--- a/src/Net/ThreadManager.cpp
+++ b/src/Net/ThreadManager.cpp
@@ -59,27 +59,27 @@ void ThreadManager::workerFunc() {
}
// And let the new worker thread join us...
- // TODO pushWork(sigc::bind(sigc::mem_fun(this, &ThreadManager::threadFinished), (gl_thread_t)gl_thread_self()));
+ pushWork(boost::bind(&ThreadManager::threadFinished, this, boost::this_thread::get_id()));
}
void ThreadManager::detach() {
- if(!isThisMainThread()) {
+ if(isThisMainThread()) {
Common::Logger::log(Common::Logger::CRITICAL, "Tried to detach main thread! This is just WRONG!");
return;
}
- runLock.lock();
- bool isRunning = running;
- runLock.unlock();
- if(!isRunning) // There's no point in creating a new worker thread when we aren't running anymore
- return;
+ {
+ boost::lock_guard<boost::mutex> lock(runLock);
+ if(!running)
+ return; // There's no point in creating a new worker thread when we aren't running anymore
+ }
threadLock.lock();
if(workerThread->get_id() == boost::this_thread::get_id()) {// Already detached?
- threads.add_thread(workerThread);
+ threads.insert(std::make_pair(boost::this_thread::get_id(), workerThread));
- workerThread = new boost::thread(std::mem_fun(&ThreadManager::workerFunc), this);
+ workerThread.reset(new boost::thread(std::mem_fun(&ThreadManager::workerFunc), this));
}
threadLock.unlock();
@@ -102,9 +102,9 @@ void ThreadManager::doInit() {
ioWorker.reset(new boost::asio::io_service::work(Connection::ioService));
mainThreadId = boost::this_thread::get_id();
- workerThread = new boost::thread(&ThreadManager::workerFunc, this);
- loggerThread = new boost::thread(&Common::LogManager::loggerThread, Common::LogManager::get());
- ioThread = new boost::thread((std::size_t(boost::asio::io_service::*)())&boost::asio::io_service::run, &Connection::ioService);
+ workerThread.reset(new boost::thread(&ThreadManager::workerFunc, this));
+ loggerThread.reset(new boost::thread(&Common::LogManager::loggerThread, Common::LogManager::get()));
+ ioThread.reset(new boost::thread((std::size_t(boost::asio::io_service::*)())&boost::asio::io_service::run, &Connection::ioService));
threadLock.unlock();
}
@@ -126,21 +126,20 @@ void ThreadManager::doDeinit() {
// We don't have to lock threadLock as detach() won't change workerThread when running is false
workerThread->join();
- delete workerThread;
// Now wait for all detached threads
- threads.join_all();
+ while(!threads.empty()) {
+ threadFinished(threads.begin()->first);
+ }
// IO thread is next
ioWorker.reset();
Connection::ioService.stop();
ioThread->join();
- delete ioThread;
// Finally, the logger thread has to die
Common::LogManager::get()->stopLoggerThread();
loggerThread->join();
- delete loggerThread;
}
}