[dmd-concurrency] Sending mesages to non-listening threads

Andrei Alexandrescu andrei at erdani.com
Tue Jan 12 14:29:35 PST 2010


Steve Schveighoffer wrote:
> ----- Original Message ----
> 
>> From: Michel Fortin <michel.fortin at michelf.com>
>>
>> It's nice that you can now send messages to any thread like this:
>>
>>     tid.send(i);
>>
>> But... what if you're sending a message to the wrong thread, to a thread that 
>> isn't listening for messages? Do the messages accumulate in memory until no 
>> memory is left? If that's the case, sending messages to the wrong thread would 
>> become a memory leak. Any solution?
> 
> Maybe a thread who doesn't want messages can close his incoming message handle?  Similar to a disconnected socket, the sender will get an exception or something (and trying to receive a message in such a thread would get an exception).

With my suggested API, a thread can close its mailbox with:

setMaxMailboxSize(0, OnCrowding.throwException);

> The other possibility is to only allow so many messages to be queued before the queue is "full".  Unsure what you want to do at that point though, either block or return an EAGAIN type error (maybe you can do both depending on how you want to send the message).  This solution is at least more likely to show logic errors, since with the first solution you may forget to close the handle.  But it also could cause deadlocks or force you to write handling code for full queue conditions.
> 
> How does erlang handle this?

I haven't found a mechanism described in the Erlang book. I think good 
Erlang style is to call receive with an "everything" handler at least in 
a while.


Andrei



More information about the dmd-concurrency mailing list