www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Why is it necessary to use synchronized functions when passing shared

Greetings

Why is it necessary to use synchronized functions when passing shared
variables? I get error even when I am not modifying the shared variable in
the function.
Kindly look at the following code. I get a compile error unless I declare
the functions parent and root synchronized.

The compile error says:
test.d(13): Error: function test.HierObject.root () const is not callable
using argument types () shared const

Thanks and Regards
- Puneet

// Reduced Code
import std.exception;

// synchronized // Compiles without error when uncommented
class HierObject {
  private shared HierObject _root;
  private shared HierObject _parent;

  shared(const(HierObject)) root() const {
    if(_root) return _root;
    else {
      enforce(_parent,
      "HierObject Instance does not have a parent!");
      return this.parent().root();
    }
  }

  shared(const(HierObject)) parent() const {
    enforce(_parent);
    return _parent;
  }
}
Mar 05 2011