Sending an immutable object to a thread
Frank Pagliughi via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat Jul 18 15:13:10 PDT 2015
OK, I found a couple of solutions, though if anyone can tell me
something better, I would love to hear it.
By making an alias to a rebindable reference, the receive() was
able to create the tuple. So I renamed the class "MessageType":
class MessageType { ... };
and then made a "Message" an immutable one of these:
alias immutable(MessageType) Message;
and finally made a "VarMessage" as a rebindable Message (thus, a
mutable reference to an immutable object):
alias Rebindable!(Message) VarMessage;
[I will likely rethink these names, but anyway... ]
Now I can send a reference to an immutable object across threads.
The receiver wants the VarMessage:
receive(
(Tid cli, VarMessage msg) {
int retVal = do_something_with(msg);
send(cli, retVal);
}
);
and a few different things work to send the object:
auto msg = new Message(...);
send(tid, thisTid(), VarMessage(msg));
or:
send(tid, thisTid(), rebindable(msg));
or:
VarMessage vmsg = new Message(...);
send(tid, thisTid(), vmsg);
A second way that seems plausible is to just make the message a
var type using a struct and then send a copy to the thread. This
seems viable since the vast bulk of the message is a string
payload, and thus the size of the struct is pretty small.
More information about the Digitalmars-d-learn
mailing list