How write a synchronized class

Discussion in 'Ruby' started by Guillaume dorchies, May 7, 2009.

  1. Hello

    How do to write a synchronized class like in java for Ruby

    I try with a Mutex but if a method call another method of the same
    instance I obtain a Deadlock.


    Thanks
    Guillaume
    --
    Posted via http://www.ruby-forum.com/.
     
    Guillaume dorchies, May 7, 2009
    #1
    1. Advertising

  2. Guillaume dorchies

    Tim Pease Guest

    On Thu, May 7, 2009 at 6:38 AM, Guillaume dorchies
    <> wrote:
    > Hello
    >
    > How do to write a synchronized class like in java for Ruby
    >
    > I try with a Mutex but if a method call another method of the same
    > instance I obtain a Deadlock.
    >
    >


    I wrote a reentrant mutex class at one point in time ...

    http://gist.github.com/108117

    Give it a shot and see if it fixes your deadlock issue.

    Blessings,
    TwP
     
    Tim Pease, May 7, 2009
    #2
    1. Advertising

  3. Guillaume dorchies, May 7, 2009
    #3
  4. 2009/5/7 Tim Pease <>:
    > On Thu, May 7, 2009 at 6:38 AM, Guillaume dorchies
    > <> wrote:
    >> Hello
    >>
    >> How do to write a synchronized class like in java for Ruby
    >>
    >> I try with a Mutex but if a method call another method of the same
    >> instance I obtain a Deadlock.
    >>
    >>

    >
    > I wrote a reentrant mutex class at one point in time ...
    >
    > http://gist.github.com/108117
    >
    > Give it a shot and see if it fixes your deadlock issue.


    Sorry, but you could have saved yourself the effort because it's part
    of the standard library:

    17:45:59 $ ruby19 -r monitor -e 'm=Monitor.new;m.synchronize {
    m.synchronize { p 1 } }'
    1
    17:46:05 $ ruby19 -r thread -e 'm=Mutex.new;m.synchronize {
    m.synchronize { p 1 } }'
    <internal:prelude>:6:in `lock': deadlock; recursive locking (ThreadError)
    from <internal:prelude>:6:in `synchronize'
    from -e:1:in `block in <main>'
    from <internal:prelude>:8:in `synchronize'
    from -e:1:in `<main>'
    17:46:17 $ ruby19 -r monitor -e 'm=Monitor.new;m.synchronize {
    m.synchronize { p 1 } }'
    1
    17:46:23 $

    Kind regards

    robert

    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
     
    Robert Klemme, May 7, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jerry
    Replies:
    4
    Views:
    131,986
    tonni
    Aug 11, 2010
  2. Pep
    Replies:
    6
    Views:
    29,352
  3. John Goche

    synchronized class

    John Goche, Jan 31, 2006, in forum: Java
    Replies:
    5
    Views:
    14,475
    Chris Uppal
    Feb 1, 2006
  4. dmcreyno
    Replies:
    9
    Views:
    9,613
    Mark Space
    Jun 27, 2006
  5. ankur
    Replies:
    4
    Views:
    1,492
    Eric Sosman
    Nov 28, 2008
Loading...

Share This Page