www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - using shared effectively in a producer/consumer situation.

reply Kevin Balbas <kbalbas gmail.com> writes:
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?
Apr 23 2017
parent reply Kevin Balbas <kbalbas gmail.com> writes:
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?
Apr 23 2017
next sibling parent Stanislav Blinov <stanislav.blinov gmail.com> writes:
On Sunday, 23 April 2017 at 20:33:48 UTC, Kevin Balbas wrote:

 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?
At the moment, pretty much yes. Either that or make the (unnecessary) immutable copies. There are no ownership primitives in Phobos yet, and shared itself is still... not quite there.
Apr 23 2017
prev sibling parent Kagamin <spam here.lot> writes:
On Sunday, 23 April 2017 at 20:33:48 UTC, Kevin Balbas wrote:
 Is this the correct way to do it?
  cast to shared, send to main thread, cast away shared?
Yes, as long as the thing is unique, you can cast it to shared or immutable just fine.
Apr 25 2017