OwnerTerminated graceful handling

Sean Kelly sean at invisibleduck.org
Wed Oct 13 10:09:03 PDT 2010


I'm not sure if you've read TDPL, but the basic idea is that if thread A spawns threads B and C, B and C should typically not outlive thread A.  Manual cleanup of spawned threads tends to be messy, so this functionality was built into the messaging system as the OwnerTerminated exception.  So currently, if a spawned thread should outlive its creator it should receive OwnerTerminated and ignore it.

I've added the beginnings of bidirectional linking as well via spawnLinked and the LinkTerminated exception, so monitor threads can be created to track the status of worker threads.  There is still a lot of functionality missing however.

Eitan Frachtenberg Wrote:

> I still don't understand how it's supposed to work. Replace the line '(OwnerTerminated) { return; }' with '(MsgToClient) { return; }', where MsgToClient is an enum, still crashes gloriously with an OwnerTerminated exception. Is there any way I can cleanly stop a thread without crashing?
>  
> On Oct 7, 2010, at 10:38 AM, Sean Kelly wrote:
> 
> > Eitan Frachtenberg Wrote:
> > 
> >> N00b question: Based on the TPDL, explicitly receive()ing OwnerTerminated prevents an exception from throwing when the owner thread terminates. Yet this is exactly what happens in the following code:
> >> 
> >> import std.concurrency;
> >> 
> >> void thr() {
> >>    receive(
> >>      (OwnerTerminated) { return; }
> >>   );
> >> }
> >> 
> >> void main() {
> >>  spawn(&thr);
> >> }
> >> 
> >> Try to wrap the receive with a try/catch pair only causes a segfault, but that's not the point anyway. What am I missing something here?
> > 
> > You're missing the fact that I forgot to implement this feature :-)  Fixed in SVN.
> 



More information about the Digitalmars-d mailing list