www.digitalmars.com         C & C++   DMDScript  

D - synchronized (anArray) { }

reply Brian Hammond <d brianhammond.com> writes:
I am trying to synchronize access to an array using the synchronized keyword.
The idea here is to disallow other threads from adding a new element to the
array while execution of another thread is in the synchronized block.  

This is disallowed as synchronized requires an Object reference not an array.  
Any thoughts?

class A { int[] n_; void foo() { synchronized (n_) {} } }

will not compile... "can only synchronize on class objects, not 'int[]'"

An obvious workaround is to use synchronized (this) {} but I was hoping for
something more fine-grained (similar for making the method synchronized).

Thanks!
Apr 21 2004
parent reply "Kris" <someidiot earthlink.dot.dot.dot.net> writes:
You can synchronize on any object, so you might do this:

class A
{
    private static Object o;
    private int[] array;

    // static constructor (nice thing in D)
    static this()
    {
           // setup a fine-grained synchronizable
           o = new Object();
    }

    void foo()
   {
         synchronized (o)
                             {
                             // do stuff
                             }
   }

    void bar()
   {
         synchronized (o)
                             {
                             // do other stuff
                             }
   }
}

"Brian Hammond" <d brianhammond.com> wrote in message
news:c66f6c$1qfd$1 digitaldaemon.com...
 I am trying to synchronize access to an array using the synchronized

 The idea here is to disallow other threads from adding a new element to

 array while execution of another thread is in the synchronized block.

 This is disallowed as synchronized requires an Object reference not an

 Any thoughts?

 class A { int[] n_; void foo() { synchronized (n_) {} } }

 will not compile... "can only synchronize on class objects, not 'int[]'"

 An obvious workaround is to use synchronized (this) {} but I was hoping

 something more fine-grained (similar for making the method synchronized).

 Thanks!

Apr 21 2004
parent reply Brian Hammond <d brianhammond.com> writes:
Ah... That makes sense. Thank you.

In article <c66fjl$1r8k$1 digitaldaemon.com>, Kris says...
You can synchronize on any object, so you might do this:

class A
{
    private static Object o;
    private int[] array;

    // static constructor (nice thing in D)
    static this()
    {
           // setup a fine-grained synchronizable
           o = new Object();
    }

    void foo()
   {
         synchronized (o)
                             {
                             // do stuff
                             }
   }

    void bar()
   {
         synchronized (o)
                             {
                             // do other stuff
                             }
   }
}

"Brian Hammond" <d brianhammond.com> wrote in message
news:c66f6c$1qfd$1 digitaldaemon.com...
 I am trying to synchronize access to an array using the synchronized

 The idea here is to disallow other threads from adding a new element to

 array while execution of another thread is in the synchronized block.

 This is disallowed as synchronized requires an Object reference not an

 Any thoughts?

 class A { int[] n_; void foo() { synchronized (n_) {} } }

 will not compile... "can only synchronize on class objects, not 'int[]'"

 An obvious workaround is to use synchronized (this) {} but I was hoping

 something more fine-grained (similar for making the method synchronized).

 Thanks!


Apr 21 2004
parent "Kris" <someidiot earthlink.dot.dot.dot.net> writes:
You might note that this approach can be useful for synchronizing several
different, but related, classes like so (or some variation on the theme);

class A
{
    private Object sync;

    this (Object sync)
   {
      this.sync = sync;
   }

   void foo ()
   {
        synchronized (synch)
                              blah blah ...
   }
}

class B
{
    private Object sync;

    this (Object sync)
   {
      this.sync = sync;
   }

   void bar ()
   {
        synchronized (synch)
                              blah blah ...
   }
}

void test()
{
    Object o = new Object();

    // both classes synchronized on a common object
    A a = new A(o);
    B b = new B(o);
}


"Brian Hammond" <d brianhammond.com> wrote in message
news:c66gt5$1tgu$1 digitaldaemon.com...
 Ah... That makes sense. Thank you.

 In article <c66fjl$1r8k$1 digitaldaemon.com>, Kris says...
You can synchronize on any object, so you might do this:

class A
{
    private static Object o;
    private int[] array;

    // static constructor (nice thing in D)
    static this()
    {
           // setup a fine-grained synchronizable
           o = new Object();
    }

    void foo()
   {
         synchronized (o)
                             {
                             // do stuff
                             }
   }

    void bar()
   {
         synchronized (o)
                             {
                             // do other stuff
                             }
   }
}

"Brian Hammond" <d brianhammond.com> wrote in message
news:c66f6c$1qfd$1 digitaldaemon.com...
 I am trying to synchronize access to an array using the synchronized

 The idea here is to disallow other threads from adding a new element to

 array while execution of another thread is in the synchronized block.

 This is disallowed as synchronized requires an Object reference not an

 Any thoughts?

 class A { int[] n_; void foo() { synchronized (n_) {} } }

 will not compile... "can only synchronize on class objects, not



 An obvious workaround is to use synchronized (this) {} but I was hoping

 something more fine-grained (similar for making the method



 Thanks!



Apr 21 2004