[dmd-concurrency] Shutdown protocol

Andrei Alexandrescu andrei at erdani.com
Wed Jan 20 11:26:40 PST 2010


Michel Fortin wrote:
> Le 2010-01-20 à 12:20, Andrei Alexandrescu a écrit :
> 
>> I like this. All - any related thoughts?
>>
>> Truth be told we can fix the example by changing main:
>>
>> wait(spawn(&copy("in1", "out1")), spawn(&copy("in2", "out2")));
>>
>> which I think is entirely fair.
> 
> As I said, (although I used 'join' instead of 'wait' to say it), using 'wait' this way will leave the writer thread alive while the second copy finishes. It's not too bad with only two copies, but if your program is long-lived and is making a lot of copy threads while it runs you'll end up with a lot of forgotten do-nothing writer threads until the program finishes.

I don't think so. When the two instances of the copy threads finish 
reading and sending messages to their writers, they return. As a 
consequence, wait returns. As a consequence, main finishes.

At this critical point, we have the following situation:

(a) main has finished, therefore the shutdown mode is in effect

(b) There are a number of messages in flight containing buffers waiting 
to be written in the two files.

The important thing to do is to NOT throw shutdown as long as calls to 
receive() would not block. As long as work is getting done and progress 
is being made, receive() will not throw.

receive() does throw, however, if a thread has a few unrelated messages 
of e.g. type string in its queue but its receive() call does not match 
string.

To summarize: Shutdown (and OOB communication in general) occurs when 
receive() would otherwise wait for a new message. (This point was not 
clear to me.)


Andrei


More information about the dmd-concurrency mailing list