2. Launching a thread Portable Headers #include <boost/thread.hpp> Launching a thread void find_the_question(intthe_answer); boost::thread deep_thought_2(find_the_question,42);
3. Launching a thread struct callable { void operator()(); }; boost::thread make_thread() { callable x; return boost::thread(x); // x is destroyed, but the newly-created thread has a copy, so this is OK }
4. Launching a thread Passing member functions to thread object m_thread = shared_ptr<thread>(new thread(bind(&CTimerThread::Execute, this)));
5. Thread Completion Join or timed_join member functions of thread object Join – waits for indefinite time for thread to complete – until the method on which the thread executes returns Timed_join takes boost::posix_time as argument m_thread->timed_join( boost::posix_time::microseconds(MAXTIMEOUT_IN_MS) );
6. Detaching a thread If boost::thread object is destroyed, the thread is detached. Detached thread continues executing until program terminates or till function returns detach() member function of boost::thread object also detaches the thread object.
7. interrupt A running thread can be interrupted by invoking the interrupt() member function of the corresponding boost::thread object. For specified interruption points like join or sleep, the interrupted thread throws boost::thread_interrupted exception. SuspendThread or ResumeThread not supported?
8. Thread ID get_id member function returns a unique id for the thread object. Portable way to sleep boost::this_thread::sleep(boost::posix_time::milliseconds(launchtime));
9. Mutex Void foo() { boost::mutex::scoped_lock l(m_mutex); //m_mutex is a boost::mutexobject //Critical section //Unlock happens automatically at end of scope }