Message Passing and Shared Data

Jacob Carlborg doob at me.com
Mon Apr 11 01:14:54 PDT 2011


On 2011-04-11 07:48, Jonathan M Davis wrote:
>> 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.
>
> There has been some talk of making it possible to do what you want to do with
> a "unique" template or attribute, but it hasn't gone anywhere that I know of.
> In particular, if it requires actual language changes, then it's unlikely to
> change in D2. We might get some sort of template that solves the problem
> though. I don't know.
>
> For the moment, however, message passing only works with immutable values. End
> of story. So, you have to do stuff like what you've been doing if you want to
> get around it. Essentially, you either deal just with immutable values, cast
> to and from immutable, or use shared. However, at the moment, there's no real
> way to "transfer ownership" of an object between threads, so message passing
> just doesn't do quite what you want to do. There may be a solution for it in
> the future though.
>
> - Jonathan M Davis

You can always create copies with serialization.

-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list