Boost logo

Boost :

From: Anthony Williams (anthony_w.geo_at_[hidden])
Date: 2007-07-05 08:50:36

"Ninel Evol" <myLC_at_[hidden]> writes:

> one thing that C++ is missing amongst many others is the
> possibility to abort a thread in a clean fashion (so that
> the thread itself, i.e. the object will do the cleanup).
> The need for this arises virtually every time you use a
> background thread for doing the work (for example
> computations).
> Currently, you can have the thread check for an abort
> request every once in a while (bad), or you can just kill
> that sucker and do the cleanup in the main thread (not that
> nice either).
> A clean alternative would be if the main thread were able to
> "provoke" an exception in the worker thread. This "abort-
> request-exception" could than be caught by the worker
> thread. It could thereby do the entire cleanup itself
> (object-oriented).

The current C++0x thread library proposal
( includes
request_cancellation(), which requests a thread cancel at the next call to
cancellation_point() or one of the standard cancellation points (such as
waiting on a condition variable). I intend to add this facility to
Boost.thread once the move to subversion is complete.

> I added a little example below. The example is for Windows
> and VCPP (sorry for that). You can do the same in Linux by
> stopping the thread via pthread_kill(id,SIGSTOP) and then
> manipulating the EIP (see thread_info.h), but of course it's
> a bit messy due to the kernel includes, etc...

This is very messy --- it's asynchronous, so will break all sorts of
stuff. POSIX asynchronous thread cancellation has successfully demonstrated
that it's a bad idea, except for very specific cases.


Anthony Williams
Just Software Solutions Ltd -
Registered in England, Company Number 5478976.
Registered Office: 15 Carrallack Mews, St Just, Cornwall, TR19 7UL

Boost list run by bdawes at, gregod at, cpdaniel at, john at