using shared effectively in a producer/consumer situation.

Kevin Balbas via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Apr 23 13:30:33 PDT 2017


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?


More information about the Digitalmars-d-learn mailing list