Concurrency Confusion

"岩倉 澪" via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Aug 7 17:39:56 PDT 2015


On Friday, 7 August 2015 at 22:13:35 UTC, 岩倉 澪 wrote:
> "message" is local to the delegate that receiveTimeout takes.
> I want to use "message" outside of the delegate in the 
> receiving thread. However, if you send an immutable value from 
> the worker thread, afaict there would be no way to assign it to 
> a global/outer variable without making a mutable copy 
> (expensive!)
> I haven't really spent much time trying to pass my "message" as 
> mutable via shared yet, but hopefully that could work...

Found the answer to this :) 
http://forum.dlang.org/post/mailman.1706.1340318206.24740.digitalmars-d-learn@puremagic.com

I send the results from my worker thread with assumeUnique, and 
then simply cast away immutable in the receiving thread like so:

(in module scope)
     Bar[] baz;

(in application loop)
     import std.array
     if(baz.empty)
     {
         import std.concurrency, std.datetime;
         receiveTimeout(0.msecs,
                 (immutable Bar[] bar){ baz = cast(Bar[])bar; });
     }



More information about the Digitalmars-d-learn mailing list