/* * ThreadManager.h * * Copyright (C) 2009 Matthias Schiffer * * This program is free software: you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by the * Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License along * with this program. If not, see . */ #ifndef MAD_CORE_THREADMANAGER_H_ #define MAD_CORE_THREADMANAGER_H_ #include "export.h" #include #include #include #include #include #include #include namespace Mad { namespace Core { class Application; class MAD_CORE_EXPORT ThreadManager : private boost::noncopyable { private: friend class Application; Application *application; boost::thread::id mainThreadId; boost::shared_ptr workerThread, loggerThread, ioThread; std::map > threads; boost::mutex threadLock; boost::mutex runLock; bool running; boost::mutex workLock; boost::condition_variable workCond; std::queue > work; boost::scoped_ptr ioWorker; ThreadManager(Application *application0); ~ThreadManager(); void workerFunc(); void ioFunc(); void threadFinished(boost::thread::id threadId) { boost::shared_ptr thread; { boost::lock_guard lock(threadLock); std::map >::iterator it = threads.find(threadId); if(it == threads.end()) return; thread = it->second; threads.erase(it); } thread->join(); } public: bool isThisMainThread() { return (mainThreadId == boost::this_thread::get_id()); } bool isThisWorkerThread() { boost::lock_guard lock(threadLock); return (workerThread->get_id() == boost::this_thread::get_id()); } void detach(); void pushWork(const boost::function0 &newWork); }; } } #endif /* MAD_CORE_THREADMANAGER_H_ */