Boost logo

Boost :

From: Tom Becker (voidampersand_at_[hidden])
Date: 2002-01-13 12:33:02


On Sun, 13 Jan 2002 11:19:41 +0100, Jens Maurer <Jens.Maurer_at_[hidden]> wrote:
>Lee Brown wrote:
>> I do have one question: why doesn't Boost.threads seem to support
>> thread cancelation? It doesn't seem to be mentioned in the documentation.
>
>Thread cancelation is difficult to do correctly in a C++ program,
>because you want to run all the destructors of stack objects of
>the thread being canceled.

Throwing an exception will do the clean up. But many thread
implementations don't provide a way to interrupt a thread and force
it to execute code (such as a throw statement) that isn't already
compiled into the thread at that point. Also, exception-safe code
depends on knowing that certain ranges of code, such as simple
copying a pointer, can never throw. Interrupting a thread can be safe
only if the cancellation exception is thrown from a point where the
code expects some exception may be thrown. Since the runtime can't
read the mind of the programmer, this means the programmer must
supply hints. The simplest approach is for the thread to occasionally
call a CheckForCancellation() function that checks a variable and
conditionally throws a cancellation exception. The thread won't get
canceled right away, but at least it will be canceled at a safe time.

By the way, we should be thankful we're using C++ and have exception
support. Think of what it would take to safely cancel a thread
without any automatic cleanup mechanism.

   Tom

-- 
Tom Becker                      "Within C++, there is a much smaller and
<voidampersand_at_[hidden]>        cleaner language struggling to get out."
                                                       -- Bjarne Stroustrup

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk