using shared effectively in a producer/consumer situation.
Kevin Balbas via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sun Apr 23 13:33:48 PDT 2017
On Sunday, 23 April 2017 at 20:30:33 UTC, Kevin Balbas wrote:
> I have an application where a long-lived "loader" thread takes
> messages to load data, loads that data, and then sends it back
> to the main thread via the standard concurrency primitives.
> Something like this:
>
> void threadFunc(Tid ownerTid)
> {
> while(true)
> {
> receive(
> (int id, string path) { LoadThingByPath(ownerTid,
> id, path); }
> );
> }
> }
>
> void LoadThingByPath(Tid ownerTid, int id, string path)
> {
> ret thing = MakeThing(path)
> send(ownerTid, id, thing);
> }
>
>
>
> I know I need to make thing shared, but I don't know how do it
> properly in this context. Casting to shared in send() works,
> but then I have to accept a shared Thing on the other side,
> process it as a shared Thing, etc. cast(Thing) doesn't appear
> to cast away the shared qualifier either. Does that mean ALL
> of the loaded data in my application needs to be shared all the
> time? Is there not simply a way to transfer ownership?
And after two hours of fiddling with this in a simple project I
notice a bug that kept me from casting away shared. Please
disregard that part.
I guess the follow up here is: Is this the correct way to do it?
cast to shared, send to main thread, cast away shared?
More information about the Digitalmars-d-learn
mailing list