Sending an immutable object to a thread
Frank Pagliughi via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Fri Jul 24 14:51:42 PDT 2015
On Friday, 24 July 2015 at 19:28:35 UTC, anonymous wrote:
> I haven't followed the discussion, so I may be missing the
> point here.
I started by asking how to send a reference to an immutable class
object from one thread to another if the reference is one of
several parameters being sent. The concurrency library can't make
a tuple for send/receive if the reference is immutable. The short
answer is (probably) to use a rebindable reference, but a
suggestion arose about the possible use of a pointer instead. So
we've devolved into a discussion of how pointers to class objects
work, and how to keep the heap memory alive as the pointer is
sent to the second thread as the original reference goes out of
scope in the first.
> A B* is not a pointer to the memory of the object. It's a
> pointer to a class reference. The class reference itself, B, is
> a pointer to the memory of the object, under the hood.
Hahaha. My forehead is red from the number of times I've thought
I've "gotten it" in this discussion.
So I think I understand that when you start to peek under the
hood, the language treats the reference and the heap memory as
distinct entities, and that they work differently for struct's
and classes.
So then: is there a pointer notation to which you can cast the
"B" reference, which thus points to the heap, but retains type
identity of the heap object?
And the reason I ask if I wanted to declare a type which is a
mutable pointer to an immutable object that is on the GC heap,
keeping type info? Or is the answer, "no", just use Rebindable
when necessary?
At this point, I'm not asking what "should" I do, but what
"could" I do.
More information about the Digitalmars-d-learn
mailing list