Sending an immutable object to a thread

Frank Pagliughi via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jul 21 13:34:29 PDT 2015


On Sunday, 19 July 2015 at 17:12:07 UTC, rsw0x wrote:
>
> a pointer to a pointer(or in this case, a reference) does not 
> keep it alive.

Interesting. If you de-reference the pointer and assign it back, 
do you get back the keep-alive? Like, in the receiving thread:

void threadFunc()
{
     receive((Tid cli, immutable(Message) *m) {
         immutable(Message) msg = *m;           // <---
         int retCode = do_something_with(msg);
         send(cli, retCode);
     });
}

I assume that even if so, there is a race condition there. You 
would need to keep the original reference alive until at least 
the "msg = *m" assignment happened, right?

Or... could you tell the GC to leave the memory alone until the 
thread gets it?  Like in the sending thread:

   Tid tid = spawn(&threadFunc);
   auto p = cast(void*) &msg;
   GC.addRoot(p);
   GC.setAttr(p, GC.BlkAttr.NO_MOVE);
   send(tid, thisTid(), &msg);
   //...

Is that possible? Is it efficient enough to do if you're sending 
lots and lots of messages?

Thanks.



More information about the Digitalmars-d-learn mailing list