Why is it necessary to use synchronized functions when passing shared variables?

d coder dlang.coder at gmail.com
Sat Mar 5 20:00:07 PST 2011


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;
  }
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20110306/5fcc3943/attachment.html>


More information about the Digitalmars-d-learn mailing list