On Mon, 17 Oct 2011 14:48:50 -0700 (PDT)
Goran <
goran...@gmail.com> wrote:
> There are functions that do not return, like the one you mention. So,
> to enforce things like RAII, you simply must not use them. Note that
> RAII is but one example, as any locally-scoped resource might be in
> danger with such calls.
>
> That said... You might be trying to terminate a thread forcibly, from
> another thread. That's poor, poor programming practice, exactly for
> reason mentioned above: you lose control of any locally-scoped
> resources and possibly other things with thread affinity. Correct way
> to "terminate" a thread is to ask it to terminate and then wait for
> it.
As it happens C++11 came close to including provision for thread
interruption; but because either cancellation or your "suicide
request" approach requires blocking system calls to be interrupted in
some way in order to be fully effective, any complete system is
intrinsically non-portable.
More generally, I think you are making too broad a statement, as either
approach (cancellation or suicide request) depends on the implementation
and in particular the platform. All modern commercial unixes and most
recent BSDs/linuxes will unwind the stack on deferred cancellation by
means of a pseudo-exception when run under a C++ environment, and this
is a good language fit. With appropriate setting of thread
cancellation status (so the thread receiving the cancellation request
can nominate its own cancellation points) it is indistinguishable in
effect from finding a way to request the receiving thread to terminate
itself and throw its own exception.
However, as I said, this is OS dependent. Getting a blocking system call
to unblock on cancellation or "finding a way to request the receiving
thread to terminate itself" when in a blocking call (say by a signal),
which is essential to many real-life use cases, is considerably easier
under unix-like OSes. Indeed windows never used to offer deferred
cancellation at all (I don't know if it does now).
Chris