Passing a class instance to a thread via spawn()

Sean Kelly sean at invisibleduck.org
Thu Jul 18 16:39:09 PDT 2013


On Jul 18, 2013, at 4:23 PM, Joseph Rushton Wakeling <joseph.wakeling at webdrake.net> wrote:

> Hello all,
> 
> I have a data structure which is a final class.  Once created, the contents of
> the class will not be mutated (only its const methods will be called).
> 
> Is there any way to pass this to a thread via spawn() or via message-passing?
> I've seen instructions to use shared() but if I try and declare the class this
> way I get errors such as "non-shared method is not callable using a shared
> object" and "non-shared const method is not callable using a shared object".
> 
> The ideal would be to mark this object, once created, as immutable -- but trusty
> methods like assumeUnique() don't work for classes!

I'd like to add move semantics of a sort which would work via something very like assumeUnique.  For now, cast the class to shared before sending, and cast away shared on receipt.  The only other option would be fore std.concurrency to copy the class when sent, but this requires serialization support.


More information about the Digitalmars-d-learn mailing list