Message Passing and Shared Data

Andrew Wiley debio264 at gmail.com
Sun Apr 10 21:45:23 PDT 2011


I'm working on an application that makes use of message passing to
separate things that need to be responsive from tasks that may take
some time, and I'm having trouble because my message passing
discipline isn't meshing with D's type system. I'm pretty much
following the contract that when I send a message from one thread to
another, the original thread doesn't store any references to the
message or what's in it, IE ownership is transferred. The receiving
thread may then send some of the same objects back after some
processing, but again, ownership is transferred.
This has the benefit that I avoid a lot of memory allocations, but it
means that right now, I'm constructing everything as shared, then
casting it away to populate the object, then sending the object, then
casting shared away again in the receiver, and so on.
The messages are generally passed around for things like requesting a
large chunk of data that takes a while to generate and receiving
objects from an IO thread that's decoding objects from sockets.

Any suggestions as to how I could avoid doing casts everywhere and/or
restore some sort of safety while avoiding having to reallocate every
object I mess with? I've tried fiddling with __gshared, but couldn't
figure out any way to use it with message passing.


More information about the Digitalmars-d mailing list