#include #include #include using namespace std; class iCallable { public: virtual void operator()(const string& name) = 0; }; typedef boost::thread cThread; typedef boost::mutex cMutex; typedef boost::lock_guard cLock; typedef map cThreadMap; cThreadMap m_threads; template void startThread(const string& thread_name) { T obj; m_threads[thread_name] = new cThread(obj, thread_name); } bool stopThread(const string& thread_name) { cThreadMap::iterator it = m_threads.find(thread_name); if (it != m_threads.end()) { it->second->interrupt(); it->second->join(); delete it->second; m_threads.erase(it); return true; } else { return false; } } class cTestRunner : public iCallable { public: virtual void operator()(const string& name) { for (;;) { boost::this_thread::sleep(boost::posix_time::microseconds(500)); } } }; int main() { startThread("a"); boost::this_thread::sleep(boost::posix_time::milliseconds(500)); stopThread("a"); return 0; }