[dmd-concurrency] Shutdown protocol

Andrei Alexandrescu andrei at erdani.com
Wed Jan 20 22:41:32 PST 2010


Sean Kelly wrote:
> On Jan 20, 2010, at 3:54 PM, Andrei Alexandrescu wrote:
> 
>> Sean Kelly wrote:
>>> On Jan 20, 2010, at 11:26 AM, Andrei Alexandrescu wrote:
>>>> 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.
>>> That sounds like a horrible race problem.
>>
>> Not at all. On the contrary, it's the stark opposite of a race:
>>
>> * Readers only finish after they have sent all messages they had to send.
>>
>> * Application doesn't shutdown until all sent messages have been acted upon.
> 
> If the reader is slower than the writer it's entirely possible that the writer will block on receive() during processing.  The receiver really only knows that it's done when it receives a message to that effect from the reader thread. If the threads were linked then this might be a "normal" shutdown message, for example.  Funny now that we're talking about this how many use cases I'm thinking of for bidirectional linking.

I don't think so. Say the reader is moving at 1 byte/millenium and the 
writer is moving at 1 petabyte/femtosecond. So the writer will spend 
virtually all of its time blocked in receive().

At the point the reader is finishing, the following actions happen in 
causal order:

1. The reader sends the last data message to the writer

2. The reader's function returns to main

3. main returns

Regardless of the relative speeds of the reader and the writer, 
causality above stays the same. There can be no race.


Andrei


More information about the dmd-concurrency mailing list