www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - does the shared keyword work with mutable structs?

reply WhatMeWorry <kheaser gmail.com> writes:
I read where shared classes have not been implemented yet. So I'm 
using
just a struct e below:  But the output below is showing that 
sharing is
not happening between Struct in main() and the various spawned 
threads.
I'm I doing something wrong?


creating queue in EventBuffer constructor
eventBuffer.enter = 1
eventBuffer.leave = 0
eB.enter = 0
eB.enter = 0
eB.leave = 0
eB.leave = 0
eB.enter = 0
eB.leave = 0


void producer(in ref shared(EventBuffer) eB, shared(Lock) lock)
{
     writeln("eB.enter = ", eB.enter);
     writeln("eB.leave = ", eB.leave);	
     Event event;
     foreach(i; 0..7)
     {
         synchronized(lock)
         {
             event.keyPressed = i;
             eB.enterOrLeaveQueue(Direction.Entering, event);
         }
     }
}

void main()
{	
     shared(Lock) lock = new shared(Lock)();

     shared(EventBuffer) eventBuffer = EventBuffer(50);
	
     writeln("eventBuffer.enter = ", eventBuffer.enter);
     writeln("eventBuffer.leave = ", eventBuffer.leave);	  	
	
     foreach(i; 0..3)
     {
         spawn(&producer, eventBuffer, lock);	
     }
}
Mar 08 2018
next sibling parent Kagamin <spam here.lot> writes:
spawn doesn't infer `ref` storage class and copies eventBuffer by 
value.
Mar 09 2018
prev sibling parent reply Kagamin <spam here.lot> writes:
To make a struct noncopyable, add  disable this(this); to it, 
then compiler will give an error on an attempt to copy it.
Mar 09 2018
parent reply WhatMeWorry <kheaser gmail.com> writes:
On Friday, 9 March 2018 at 10:42:47 UTC, Kagamin wrote:
 To make a struct noncopyable, add  disable this(this); to it, 
 then compiler will give an error on an attempt to copy it.
I tried the disable this(this); but now it doesn't even compile? Error: template std.concurrency.spawn cannot deduce function from argument types !()(void function(shared(EventBuffer) eB, shared(Lock) lock), shared(EventBuffer), shared(Lock)), candidates are: C:\ldc2\bin\..\import\std\concurrency.d(464): std.concurrency.spawn(F, T...)(F fn, T args) if (isSpawnable!(F, T)) Is std.concurrency a work in progress or I'm I just obtuse here? I've been reading Ali's book on the concurrency chapters as inspiration, but the examples there use simple data types like ints or bools.
Mar 09 2018
next sibling parent Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
On Friday, March 09, 2018 19:33:26 WhatMeWorry via Digitalmars-d-learn 
wrote:
 On Friday, 9 March 2018 at 10:42:47 UTC, Kagamin wrote:
 To make a struct noncopyable, add  disable this(this); to it,
 then compiler will give an error on an attempt to copy it.
I tried the disable this(this); but now it doesn't even compile? Error: template std.concurrency.spawn cannot deduce function from argument types !()(void function(shared(EventBuffer) eB, shared(Lock) lock), shared(EventBuffer), shared(Lock)), candidates are: C:\ldc2\bin\..\import\std\concurrency.d(464): std.concurrency.spawn(F, T...)(F fn, T args) if (isSpawnable!(F, T)) Is std.concurrency a work in progress or I'm I just obtuse here? I've been reading Ali's book on the concurrency chapters as inspiration, but the examples there use simple data types like ints or bools.
Well, as Kagamin said, the compiler gives you an error if you disable this(this); and then the code attempts to copy the type. The point was to catch when the type was copied, not make spawn work with your type. spawn requires that the types that its given be copyable. Either your type needs to be able to work if it's copied, or it needs to be a reference type (either by using a class, a pointer to a struct, or by making the struct's guts live on the heap with a member variable that's a pointer pointing to them). - Jonathan M Davis
Mar 09 2018
prev sibling parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 3/9/18 2:33 PM, WhatMeWorry wrote:
 On Friday, 9 March 2018 at 10:42:47 UTC, Kagamin wrote:
 To make a struct noncopyable, add  disable this(this); to it, then 
 compiler will give an error on an attempt to copy it.
I tried the disable this(this); but now it doesn't even compile?
Use pointers for passing: void producer(in shared(EventBuffer) *eB, shared(Lock) lock) ... spawn(&producer, &eventBuffer, lock); The disable postblit is to prevent you from doing what you did -- passing a value type expecting it gets passed by reference. In order to avoid the error you must use a reference.
 
 Is std.concurrency a work in progress or I'm I just obtuse here?
It's not a work in progress, though there are always chances there are bugs there. We fixed one not too long ago in which it didn't work with certain types.
 I've been reading Ali's book on the concurrency chapters as inspiration, 
 but the examples there use simple data types like ints or bools.
TBH, the best things to pass as messages are value types or immutables. But obviously, if you need to share something, you need to use shared. -Steve
Mar 13 2018