does the shared keyword work with mutable structs?

WhatMeWorry kheaser at gmail.com
Thu Mar 8 23:14:08 UTC 2018


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);	
     }
}


More information about the Digitalmars-d-learn mailing list