[dmd-concurrency] Shutdown protocol

Michel Fortin michel.fortin at michelf.com
Wed Jan 20 20:50:42 PST 2010


Le 2010-01-20 à 20:54, Andrei Alexandrescu a écrit :

> Michel Fortin wrote:
> 
>> You can easily transform the file copy program to add the race condition though. The file copy example has one reading and one writing thread. Now let's add a processing thread in the middle that does something to the data (could be compression or encryption):
>> 	reading -> processing -> writing
>> Once the reading thread has finished, some parts will be in the process of being processed, other parts will be in the process of being written. Now, assume writing is faster than processing: the writing thread will finish what it has and wait for new messages (those parts the processing thread is still processing), but at that moment it will be interrupted by a shutdown. After that, while the processing thread slowly advance with its processing it sends messages to a dead writing thread.
> 
> I have no doubt many racy scenarios exist out there. In fact any given shutdown protocol is more likely to be racy than not. That's why the fact that there are actually non-racy applications that are easy to write and understand is remarkable.

I hadn't thought of this race problem when I said we could leave the shutdown protocol like this earlier. I'm not so sure it's not a problem. But I'd like to see what are the goals of the shutdown protocol, this might provide a better way to evaluate it than just discussing its effect randomly.

Anyway, here are my random thoughts...

I would say that the example in the current draft is encouraging a bad practice by relying on the shutdown protocol for cleanup. You need to get rid of the writer thread when its task is done, not when the process finishes. In this simple example both happens at the same time so it works, but copy-paste the code elsewhere and it'll leave an idle writer thread until the program shutdowns.

A thread with a never ending task (listening to a socket in a server app for instance) might be a better example if you wanted to show off the shutdown protocol.


> About your example: in such a pipeline, I'd definitely define a simple protocol to signal termination, for example by sending an empty buffer and waiting for an ack in the reader.

You don't really need an ack, just wait() for the thread to terminate after sending the empty buffer. And you don't really need an empty buffer, just send a shutdown exception yourself.


> That's actually a great example for the book!

I agree.


-- 
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/





More information about the dmd-concurrency mailing list