Boost threads

1,469 views

Published on

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,469
On SlideShare
0
From Embeds
0
Number of Embeds
22
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Boost threads

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

×