Thread synchronization

JohnC johnch_atms at hotmail.com
Wed Nov 29 16:36:52 PST 2006


What's the recommended way to guard static variables?

1) synchronized by itself?

  static Object getObj() {
    synchronized {
      return sharedVar;
    }
  }

2) synchronized on classinfo?

  static Object getObj() {
    synchronized (typeof(sharedVar).classinfo) {
      return sharedVar;
    }
  }

3) synchronized on a static Object instance?

  static this() {
    syncLock = new Object;
  }

  static Object getObj() {
    synchronized (syncLock) {
      return sharedVar;
    }
  }

I've seen all these patterns used. Are they all safe?

Cheers,
John. 





More information about the Digitalmars-d-learn mailing list