Sending an immutable object to a thread

Frank Pagliughi via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jul 19 10:04:04 PDT 2015


> It looks like passing a pointer to an immutable(Message) works 
> as well:

Oh, yes, pointer. Ha! I didn't even think of that. Thanks.

I'm not familiar with how garbage collection works in D. If the 
initial reference goes out of scope, and you just have a pointer 
- in another thread, no less - then are you still guaranteed that 
the object will not disappear while the pointer exists?

Like if I did something akin to:

// ...like before...

void send_msg(Tid tid, int n)
{
     auto msg = new immutable(Message)(n);
     send(tid, thisTid(), &msg);
}

void main()
{
     Tid tid = spawn(&threadFunc);
     send_msg(tid, 100);
     receiveOnly!int();
}

Do I know that the message object won't be garbage collected 
before the thread finishes with it? (I realize this is a very 
artificial example, but something like this could happen in a 
bigger library).

Frank


More information about the Digitalmars-d-learn mailing list