Sending an immutable object to a thread

Frank Pagliughi via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Jul 18 07:55:02 PDT 2015


Hey All,

I'm trying to send immutable class objects to a thread, and am 
having trouble if the object is one of several variables sent to 
the thread. For example, I have a "Message" class:

     class Message { ... }

and I create an immutable object from it, and send it to another 
thread:

     auto msg = immutable Message(...);

     Tid tid = spawn(&threadFunc);
     send(tid, thisTid(), msg);

I then attempt to receive it in the threadFunc like:

     receive(
         (Tid cli, immutable Message msg) {
             int retCode = do_something_with(msg);
             send(cli, retCode);
         }
     );

I get compilation errors about the inability of building the 
tuple, like:
/usr/include/dmd/phobos/std/variant.d(346): Error: cannot modify 
struct *zat Tuple!(Tid, immutable(Message)) with immutable members
/usr/include/dmd/phobos/std/variant.d(657): Error: template 
instance std.variant.VariantN!32LU.VariantN.handler!(Tuple!(Tid, 
immutable(Message))) error instantiating
/usr/include/dmd/phobos/std/variant.d(580):        instantiated 
from here: opAssign!(Tuple!(Tid, immutable(Message)))
/usr/include/dmd/phobos/std/concurrency.d(124):        
instantiated from here: __ctor!(Tuple!(Tid, immutable(Message)))
/usr/include/dmd/phobos/std/concurrency.d(628):        
instantiated from here: __ctor!(Tid, immutable(Message))
/usr/include/dmd/phobos/std/concurrency.d(618):        ... (1 
instantiations, -v to show) ...
/usr/include/dmd/phobos/std/concurrency.d(594):        
instantiated from here: _send!(Tid, immutable(Message))
MsgTest.d(92):        instantiated from here: send!(Tid, 
immutable(Message))

I tried various combinations of using Rebindable, but couldn't 
get anything to compile.

Thanks.


More information about the Digitalmars-d-learn mailing list