# Synchronized object

**URL:** <https://rubytalk.org/t/synchronized-object/26821>\
**Category:** ruby-talk\
**Created:** [21 April 2006 14:47 UTC](https://rubytalk.org/t/synchronized-object/26821 "2006-04-21T14:47:56Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kroeger\_Simon\_ext](https://avatars.discourse-cdn.com/v4/letter/k/5daacb/32.png) [@Kroeger\_Simon\_ext](https://rubytalk.org/u/Kroeger_Simon_ext)\
**Post date:** [21 April 2006 14:47 UTC](https://rubytalk.org/t/synchronized-object/26821/1 "2006-04-21T14:47:56Z")

</div>

Dear ruby-talk,

is there a nice, concise way to lock each and  
every method against all other methods of an object?

To elaborate: I want to make sure that only one method of  
a specific object (not class) can run at a each point in  
time regardless of how many threads are using this object.

I would hope there is module i just have to include in  
my class definition.

cheers and thanks

Simon

---

<div class="post-metadata">

**Author:** ![Farrel\_Lifson](https://avatars.discourse-cdn.com/v4/letter/f/3da27b/32.png) [@Farrel\_Lifson](https://rubytalk.org/u/Farrel_Lifson)\
**Post date:** [21 April 2006 15:21 UTC](https://rubytalk.org/t/synchronized-object/26821/2 "2006-04-21T15:21:03Z")

</div>

You could have a class wide Mutex.

class MyClass  
&nbsp;&nbsp;@@mutex = Mutex.new  
&nbsp;&nbsp;def atomicy\_method  
&nbsp;&nbsp;&nbsp;&nbsp;@@mutex.synchronise do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Code here  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
end

Farrel

---

<div class="post-metadata">

**Author:** ![David\_Pollak1](https://avatars.discourse-cdn.com/v4/letter/d/dec6dc/32.png) [@David\_Pollak1](https://rubytalk.org/u/David_Pollak1)\
**Post date:** [21 April 2006 15:28 UTC](https://rubytalk.org/t/synchronized-object/26821/3 "2006-04-21T15:28:27Z")

</div>

Simon,

Look at the Monitor module.

For example:  
require 'monitor'

class Foo  
&nbsp;&nbsp;include MonitorMixin

&nbsp;&nbsp;def bar  
&nbsp;&nbsp;&nbsp;&nbsp;self.synchronized do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# stuff  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end

&nbsp;&nbsp;def baz  
&nbsp;&nbsp;&nbsp;&nbsp;self.synchronized do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# more stuff  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
end

It's a little less syntactically sugary than Java, but about the same as C#.

Do this help?

Thanks,

David

> **···**
>
> On 4/21/06, Kroeger, Simon (ext) \<simon.kroeger.ext@siemens.com\> wrote:
> 
> > Dear ruby-talk,
> > 
> > is there a nice, concise way to lock each and  
> > every method against all other methods of an object?
> > 
> > To elaborate: I want to make sure that only one method of  
> > a specific object (not class) can run at a each point in  
> > time regardless of how many threads are using this object.
> > 
> > I would hope there is module i just have to include in  
> > my class definition.
> > 
> > cheers and thanks
> > 
> > Simon

---

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [21 April 2006 15:32 UTC](https://rubytalk.org/t/synchronized-object/26821/4 "2006-04-21T15:32:17Z")

</div>

You could wrap it in a delegate object you strip of methods, then just have method\_missing() forward the messages inside a synchronize block.

James Edward Gray II

> **···**
>
> On Apr 21, 2006, at 9:47 AM, Kroeger, Simon (ext) wrote:
> 
> > Dear ruby-talk,
> > 
> > is there a nice, concise way to lock each and  
> > every method against all other methods of an object?
> > 
> > To elaborate: I want to make sure that only one method of  
> > a specific object (not class) can run at a each point in  
> > time regardless of how many threads are using this object.
> > 
> > I would hope there is module i just have to include in  
> > my class definition.

---

<div class="post-metadata">

**Author:** ![Logan\_Capaldo](https://avatars.discourse-cdn.com/v4/letter/l/7ea924/32.png) [@Logan\_Capaldo](https://rubytalk.org/u/Logan_Capaldo)\
**Post date:** [21 April 2006 15:33 UTC](https://rubytalk.org/t/synchronized-object/26821/5 "2006-04-21T15:33:39Z")

</div>

Well it's not quite as easy as Java but:  
[http://www.ruby-doc.org/stdlib/libdoc/monitor/rdoc/index.html](http://www.ruby-doc.org/stdlib/libdoc/monitor/rdoc/index.html)

obj.methods.each do |meth|  
&nbsp;&nbsp;&nbsp;&nbsp;(class \<\< obj; self; end).module\_eval \<\<HERE  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def #{meth}(\*args, &block)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.synchronize { super }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
HERE

end

obj.extend(MonitorMixin)

I did it in that order since I wouldn't want a synchronize { synchronize }

> **···**
>
> On Apr 21, 2006, at 10:47 AM, Kroeger, Simon (ext) wrote:
> 
> > Dear ruby-talk,
> > 
> > is there a nice, concise way to lock each and  
> > every method against all other methods of an object?
> > 
> > To elaborate: I want to make sure that only one method of  
> > a specific object (not class) can run at a each point in  
> > time regardless of how many threads are using this object.
> > 
> > I would hope there is module i just have to include in  
> > my class definition.
> > 
> > cheers and thanks
> > 
> > Simon

---

<div class="post-metadata">

**Author:** ![Andy\_Gimblett](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@Andy\_Gimblett](https://rubytalk.org/u/Andy_Gimblett)\
**Post date:** [21 April 2006 15:34 UTC](https://rubytalk.org/t/synchronized-object/26821/6 "2006-04-21T15:34:39Z")

</div>

If you want something like you get in Java with the "synchronized"  
keyword, that's a monitor, and the standard library includes  
(the not terribly well documented) monitor.rb:

[http://www.ruby-doc.org/stdlib/libdoc/monitor/rdoc/](http://www.ruby-doc.org/stdlib/libdoc/monitor/rdoc/)

(also [Monitor (synchronization) - Wikipedia](http://en.wikipedia.org/wiki/Monitor_%28synchronization%29) )

Whether it's nice or concise I don't know - it's certainly not a  
"one-liner" way to achieve what you want but it might get you a long  
way along the road. If you use it, I'd personally be interested in  
how you got on.

(Caveat: I haven't used this, I just saw "synchronized object" in your  
subject line, thought "monitor", and searched the web for "ruby  
monitor synchronization" which led me to this info.)

Hope this helps,

-Andy

> **···**
>
> On Fri, Apr 21, 2006 at 11:47:56PM +0900, Kroeger, Simon (ext) wrote:
> 
> > Dear ruby-talk,
> > 
> > is there a nice, concise way to lock each and every method against  
> > all other methods of an object?
> > 
> > To elaborate: I want to make sure that only one method of a specific  
> > object (not class) can run at a each point in time regardless of how  
> > many threads are using this object.
> > 
> > I would hope there is module i just have to include in my class  
> > definition.
> 
> --  
> Andy Gimblett  
> Computer Science Department  
> University of Wales Swansea  
> [http://www.cs.swan.ac.uk/~csandy/](http://www.cs.swan.ac.uk/~csandy/)

---

<div class="post-metadata">

**Author:** ![David\_Corbin1](https://avatars.discourse-cdn.com/v4/letter/d/8491ac/32.png) [@David\_Corbin1](https://rubytalk.org/u/David_Corbin1)\
**Post date:** [22 April 2006 00:15 UTC](https://rubytalk.org/t/synchronized-object/26821/7 "2006-04-22T00:15:34Z")

</div>

It would be relatively easy to write a class that uses Monitor and delegates  
to specifc object. Thus, much like Java's Collections.synchronizedFoo, you  
could have

Synchronizer.new(foo) that would synchronize ANY object.

David

> **···**
>
> On Friday 21 April 2006 10:47 am, Kroeger, Simon (ext) wrote:
> 
> > Dear ruby-talk,
> > 
> > is there a nice, concise way to lock each and  
> > every method against all other methods of an object?
> > 
> > To elaborate: I want to make sure that only one method of  
> > a specific object (not class) can run at a each point in  
> > time regardless of how many threads are using this object.
> 
> > I would hope there is module i just have to include in  
> > my class definition.
> > 
> > cheers and thanks
> > 
> > Simon

---

<div class="post-metadata">

**Author:** ![Simon\_Kroger](https://avatars.discourse-cdn.com/v4/letter/s/49beb7/32.png) [@Simon\_Kroger](https://rubytalk.org/u/Simon_Kroger)\
**Post date:** [21 April 2006 19:23 UTC](https://rubytalk.org/t/synchronized-object/26821/8 "2006-04-21T19:23:32Z")

</div>

Logan Capaldo wrote:

> Well it's not quite as easy as Java but:  
> [http://www.ruby-doc.org/stdlib/libdoc/monitor/rdoc/index.html](http://www.ruby-doc.org/stdlib/libdoc/monitor/rdoc/index.html)
> 
> obj.methods.each do |meth|  
> &nbsp;&nbsp;&nbsp;(class \<\< obj; self; end).module\_eval \<\<HERE  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def #{meth}(\*args, &block)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.synchronize { super }  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> HERE
> 
> end
> 
> obj.extend(MonitorMixin)
> 
> I did it in that order since I wouldn't want a synchronize { synchronize }

accepted answer ... well kind of. 😉

running your code (with an empty array) gave me:

ruby 1.8.4 (2005-12-24) [i386-mswin32]

(eval):1: warning: redefining `\_\_send\_\_' may cause serious problem  
(eval):1: warning: redefining `\_\_id\_\_' may cause serious problem  
(eval):2:in `extend': undefined method `synchronize' for  
#\<Array:0x2816468\> (NoMethodError)

but i got the right idea i think. My code now looks like:

> **···**
>
> ---------------------------------------------------------------------  
> require 'monitor'
> 
> def sync obj  
> &nbsp;&nbsp;meth = obj.methods - %w{\_\_send\_\_ \_\_id\_\_}  
> &nbsp;&nbsp;klass = class \<\< obj.extend(MonitorMixin); self; end  
> &nbsp;&nbsp;meth.each{|m|klass.module\_eval "def #{m}(\*a,&b)synchronize{super}end"}  
> &nbsp;&nbsp;obj  
> end
> 
> ---------------------------------------------------------------------
> 
> usage:  
> ---------------------------------------------------------------------  
> obj = sync()  
> obj.push 'a'  
> p obj  
> ---------------------------------------------------------------------
> 
> and now that i see it, it looks quite simple 🙂
> 
> thanks to all of you who responded
> 
> cheers
> 
> Simon

---

<div class="post-metadata">

**Author:** ![Patrick\_Hurley1](https://avatars.discourse-cdn.com/v4/letter/p/b77776/32.png) [@Patrick\_Hurley1](https://rubytalk.org/u/Patrick_Hurley1)\
**Post date:** [22 April 2006 02:54 UTC](https://rubytalk.org/t/synchronized-object/26821/9 "2006-04-22T02:54:14Z")

</div>

require 'monitor'

class AutoMonitor  
&nbsp;&nbsp;include MonitorMixin

&nbsp;&nbsp;def initialize(obj)  
&nbsp;&nbsp;&nbsp;&nbsp;mon\_initialize  
&nbsp;&nbsp;&nbsp;&nbsp;@obj = obj  
&nbsp;&nbsp;end

&nbsp;&nbsp;def method\_missing(sym, \*args, &block)  
&nbsp;&nbsp;&nbsp;&nbsp;synchronize do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@obj.send(sym, \*args, &block)  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
end

class Class  
&nbsp;&nbsp;def monitored\_new(\*args, &block)  
&nbsp;&nbsp;&nbsp;&nbsp;AutoMonitor.new(new(\*args, &block))  
&nbsp;&nbsp;end  
end

Something like this and then you can:

f = Foo.monitored\_new

pth

> **···**
>
> On 4/21/06, David Corbin \<dcorbin@machturtle.com\> wrote:
> 
> > On Friday 21 April 2006 10:47 am, Kroeger, Simon (ext) wrote:  
> > \> Dear ruby-talk,  
> > \>  
> > \> is there a nice, concise way to lock each and  
> > \> every method against all other methods of an object?  
> > \>  
> > \> To elaborate: I want to make sure that only one method of  
> > \> a specific object (not class) can run at a each point in  
> > \> time regardless of how many threads are using this object.
> > 
> > \> I would hope there is module i just have to include in  
> > \> my class definition.  
> > \>  
> > \> cheers and thanks  
> > \>  
> > \> Simon
> > 
> > It would be relatively easy to write a class that uses Monitor and delegates  
> > to specifc object. Thus, much like Java's Collections.synchronizedFoo, you  
> > could have
> > 
> > Synchronizer.new(foo) that would synchronize ANY object.
> > 
> > David

---

<div class="post-metadata">

**Author:** ![Simon\_Kroger](https://avatars.discourse-cdn.com/v4/letter/s/49beb7/32.png) [@Simon\_Kroger](https://rubytalk.org/u/Simon_Kroger)\
**Post date:** [22 April 2006 12:23 UTC](https://rubytalk.org/t/synchronized-object/26821/10 "2006-04-22T12:23:28Z")

</div>

Thanks a lot, but consider this:

> **···**
>
> -------------------------------------------------------------------------  
> require 'monitor'
> 
> class AutoMonitor  
> &nbsp;&nbsp;include MonitorMixin
> 
> &nbsp;&nbsp;def initialize(obj)  
> &nbsp;&nbsp;&nbsp;&nbsp;mon\_initialize  
> &nbsp;&nbsp;&nbsp;&nbsp;@obj = obj  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;def method\_missing(sym, \*args, &block)  
> &nbsp;&nbsp;&nbsp;&nbsp;synchronize do  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@obj.send(sym, \*args, &block)  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end  
> end
> 
> def sync obj  
> &nbsp;&nbsp;meth = obj.methods - %w{\_\_send\_\_ \_\_id\_\_}  
> &nbsp;&nbsp;klass = class \<\< obj.extend(MonitorMixin); self; end  
> &nbsp;&nbsp;meth.each{|m|klass.module\_eval "def #{m}(\*a,&b)synchronize{super}end"}  
> &nbsp;&nbsp;obj  
> end
> 
> hash = {"test1" =\> 1, AutoMonitor.new("test2") =\> 2, sync('test3') =\> 3}
> 
> p hash.include?("test1") #=\> true  
> p hash.include?(AutoMonitor.new("test1")) #=\> false  
> p hash.include?("test2") #=\> false  
> p hash.include?(AutoMonitor.new("test2")) #=\> false
> 
> #but
> 
> p hash.include?("test1") #=\> true  
> p hash.include?(sync("test1")) #=\> true  
> p hash.include?("test3") #=\> true  
> p hash.include?(sync("test3")) #=\> true  
> -------------------------------------------------------------------------
> 
> Of course it is possible to enhance AutoMonitor to handle these cases,  
> but i like the 4 liner. 🙂
> 
> If only i could make this module\_eval go away. (or at least use the  
> block form)
> 
> cheers
> 
> Simon
> 
> Patrick Hurley wrote:
> 
> > On 4/21/06, David Corbin \<dcorbin@machturtle.com\> wrote:  
> > [...]  
> > require 'monitor'
> > 
> > class AutoMonitor  
> > &nbsp;&nbsp;include MonitorMixin
> > 
> > &nbsp;&nbsp;def initialize(obj)  
> > &nbsp;&nbsp;&nbsp;&nbsp;mon\_initialize  
> > &nbsp;&nbsp;&nbsp;&nbsp;@obj = obj  
> > &nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;def method\_missing(sym, \*args, &block)  
> > &nbsp;&nbsp;&nbsp;&nbsp;synchronize do  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@obj.send(sym, \*args, &block)  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;end  
> > end
> > 
> > class Class  
> > &nbsp;&nbsp;def monitored\_new(\*args, &block)  
> > &nbsp;&nbsp;&nbsp;&nbsp;AutoMonitor.new(new(\*args, &block))  
> > &nbsp;&nbsp;end  
> > end
> > 
> > Something like this and then you can:
> > 
> > f = Foo.monitored\_new
> > 
> > pth
