Concurrency Confusion
Chris via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat Aug 8 06:34:23 PDT 2015
On Saturday, 8 August 2015 at 00:39:57 UTC, 岩倉 澪 wrote:
> 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; });
> }
Note aside: if you only import what you need (say `import
std.concurrency : receiveTimeout; std.datetime : msecs`), you can
reduce the size of the executable considerably as your program
grows.
More information about the Digitalmars-d-learn
mailing list