Forum: Ruby How write a synchronized class

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Guillaume E. (Guest)
on 2009-05-07 16:38
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
Tim P. (Guest)
on 2009-05-07 18:38
(Received via mailing list)
On Thu, May 7, 2009 at 6:38 AM, Guillaume dorchies
<removed_email_address@domain.invalid> 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
Guillaume E. (Guest)
on 2009-05-07 18:42
Thank you It's exactly that I need
Robert K. (Guest)
on 2009-05-07 19:48
(Received via mailing list)
2009/5/7 Tim P. <removed_email_address@domain.invalid>:
>
> 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
This topic is locked and can not be replied to.