www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - __gshared & class instances

Hi everyone,

I'm having some trouble by using threads. I've 3 classes:

class MainClass {
...

   __gshared {
      TimedCallback callback;
      SecondClass instance;
   }
   void callbackMethod() {...}
   void load() {
      // loads something... and do:
      instance = new SecondClass();
      callback = new TimedCallback(20, &callbackMethod);
   }
   this() {
      Thread t = new Thread(&load);
      t.start();
   }
...
}

class SecondClass {
...
   __gshared TimedCallback callback;
   void otherCallback() {...}
   this() {
      // loads something... and do:
      callback = new TimedCallback(5, &otherCallback);
   }

   void callMe() {
      // For testing purposes only:
      throw new Exception(std.conv.to!(string)(callback.interval));
   }
...
}

class TimedCallback {

   __gshared {
      uint pinterval;
      void delegate() callback;
   }

   this(uint ival, void delegate() cb) {
      pinterval = ival;
      callback = cb;
   }

    property {
      uint interval() { return pinterval; }
   }

}

What I'm doing is: I have a class where the constructor creates an thread to
load a other class. Both classes contains a member of
TimedCallback. When the thread finished, I call the callMe()-Method of the
SecondClass-Instance using my mainthread. callMe throws an
interval of 20 and not 5 (as needed) - it seems that the instance of
TimedCallback, created using the SecondClass-constructor will be
overwritten by the MainClass at the line of "callback = new TimedCallback(20,
&callbackMethod);".

I hope anything knows what I'm doing wrong :)
May 29 2011