Questions about Chapter 18 (Boost Thread)

Volume I - Foundations

Questions about Chapter 18 (Boost Thread)

Postby admin » Mon Feb 21, 2011 5:06 pm

We've got a few questions from a reader about chapter 18 which we will answer below.

[Q1] In Section 18.8, you talk about interrupts. In the code fragment you have a try block with a function call boost::this_thread::interruption_point()

Am I right in saying that unless the function t.interrupt() is invoked from the main function (in this case) this is ignored. So if you removed the t.interrupt() call that thread in the function ThreadFunction will be stuck in a infinite loop?

Yes, a call to interruption_point() is ignored. It has no functionality besides throwing a thread_interrupted exception in case the thread was interrupted. A thread_interrupted exception can only be thrown when the thread is in the ?WaitSleepJoin? state thus when the boost threading functions sleep(), wait() or join() is called. So if the loop never calls one of those functions, it will never be interrupted. In that case you need to call interruption_point() so a thread can be interrupted else it will be in this case an infinite loop. And if you never call t.interrupt(), the thread function will be an infinite loop too in this case.

[Q2] In Section 18.12, how does the Producer thread break out of its while(true) loop? I see no call to boost::this_thread::interruption_point(); as in the Consumer thread's while(true) loop?

See also answer above. Threads can be interrupted only when they are in the ?WaitSleepJoin? state. Since the producer loop calls sleep(), it can be interrupted there. So a call to interruption_point() is not needed in this case.

[Q3] Again in Section 18.12, you make a call to boost::this_thread::interruption_point();

however, you mention in Section 18.8 that this will throw an exception. In the main() function you do not have any catch statements for this exception. Does this mean your program will crash? Admittedly I have not run it (don't have the facilities at the moment), or do the function calls:

// Interrupt the threads and stop them

producers.interrupt_all(); producers.join_all(); consumers.interrupt_all(); consumers.join_all();

handle the exception?

Exceptions thrown inside a thread can only be handled in the thread they are thrown in. So the main() function does not handle them but must be handled in the thread function themselves. If the thread function does not handle it, then that thread will be stopped but the remaining threads and main program will still continue running. The regular ?exception not handled? messages are not shown for unhandled exceptions in additional threads. So the program will not crash, only that thread will stop.

It is however more clean to catch the exception. So in the while loops, you can place the code in a try block and add a catch handler for thread_interrupted exception to cleanly abort the thread (or to cancel the interrupt and continue running).

[Q4] Finally, still referring to Section 18.8, if boost::this_thread::interruption_point(); throws an exception, why are they not in a try block in the Consumer and Producer threads?

See also the answer above. In this case to simplify the example. But in real-world code, you would put it in a try?catch block to handle the exception cleanly.
Site Admin
Posts: 237
Joined: Thu Dec 07, 2006 4:03 pm
Location: Amsterdam, The Netherlands

Re: Questions about Chapter 18 (Boost Thread)

Postby saaleha » Thu Apr 23, 2015 8:13 am

Here I focus specifically on how to use Boost threads in a practical setting, ... There is a separate example program for each section below, and a common Bjam ... This is simply to avoid cluttering up the examples with code that would .....
Posts: 1
Joined: Thu Apr 23, 2015 6:55 am

Return to Introduction to the Boost C++ Libraries - Volume I (Demming/Duffy)

Who is online

Users browsing this forum: No registered users and 1 guest