[dmd-concurrency] Shutdown protocol

Michel Fortin michel.fortin at michelf.com
Wed Jan 20 11:24:34 PST 2010


Le 2010-01-20 à 13:59, Robert Jacques a écrit :

> On Wed, 20 Jan 2010 13:40:47 -0500, Michel Fortin <michel.fortin at michelf.com> wrote:
>> Le 2010-01-20 à 12:40, Robert Jacques a écrit :
>> 
>>> I like the updated example, but the updated shutdown protocol sounds like a race waiting to happen. For example, suppose you spawn a thread (A) and send it's tid to another thread (B) and then exit. The spawned thread dies and the other then runs into errors. So now B's racing on A's shutdown event and may have intended behavior one day and then throw a bunch of shutdown exceptions the next.
>> 
>> Indeed. That's a bad thing about it.
>> 
>> 
>>> However, expanding on this concept, if tid was ref-counted, shutdown exceptions could be thrown correctly and at least the above race would be avoided.
>> 
>> Although with reference counting two or more threads referencing each other would create circular references and never shutdown. In some cases perhaps that's what you want though, if you use another shutdown mechanism for instance, but you probably don't want this be implicit when creating circular references.
> 
> I thought about that, but I couldn't actually come up with a real and still pathological circular construct: unlike with garbage, a thread normally self-terminates. So cycles end up being naturally broken. The only time you'd end up with a true circular reference situation is two threads sending and receiving from each other inside infinite loops. Which I'd define as a logic bug and an uncommon situation. You could always add manual shutdown calls, to handle these situations.

Consider this case with a Dispatcher and a Worker thread communicating with each other like this:

Request:  Clients -> Dispatcher Thread -> Worker Thread
Response: Clients <- Dispatcher Thread <- Worker Thread

Clients send requests to a dispatcher thread. Dispatcher decides to which worker to give the request. Worker does its work, sends the response to the dispatcher, which sends it back to the client. Here the dispatcher is in an infinite loop waiting for client requests, the worker thread is in an infinite loop waiting for requests from the dispatcher, and both know each other. Here you have your circular reference.

Is this a logic bug? I don't think so.

It's true that you don't want those two threads to shutdown by themselves in this case, but I'm a little uneasy about this being a "feature" of reference counting.

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





More information about the dmd-concurrency mailing list