From: John Max Skaller (skaller_at_[hidden])
Date: 2001-08-15 15:02:18
Scott McCaskill wrote:
> Shouldn't thread::join() throw an exception if a thread tries to join
> itself? Right now, it will deadlock in such a situation.
Interesting. But the answer is probably NO.
The reason is: consider a thread A trying to join a thread
B which is itself non-terminating. That can happen if B
is in an infinite loop, or itself deadlocked. In this case,
there is no way to detect that the call to join will never
On the other hand, in BOTH cases, cancellation,
if it were introduced, would end the deadlock, on a
thread by thread basis (assuming that 'join' is a cancellation
point). So there's a considerably danger that a valid
idiom would be defeated by prematurely throwing an exception:
that is, it isn't necessarily an error.
Indeed, one could argue differently, that a
thread is _already_ joined to itself, and the call
should just return! For example: consider a pool
of threads that should all die together: each thread
just joins the whole pool (including itself).
It is dangerous to throw exceptions
in those special cases a condition is detectable,
whilst not doing so in other cases where the
condition is not, because catching exceptions
is a valid programming technique, and the
programmer might be surprised, for example,
that not all deadlocks are caught.
Generally, 'special cases' are the enemy of good design.
They usually only work in very particular contexts
and fail when the context is changed or extended.
-- John (Max) Skaller, mailto:skaller_at_[hidden] 10/1 Toxteth Rd Glebe NSW 2037 Australia voice: 61-2-9660-0850 New generation programming language Felix http://felix.sourceforge.net Literate Programming tool Interscript http://Interscript.sourceforge.net
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk