# Monitor Error when Joining Threads

**URL:** <https://www.ruby-forum.com/t/monitor-error-when-joining-threads/144131>\
**Category:** Ruby\
**Created:** [July 25, 2008, 5:50pm UTC](https://www.ruby-forum.com/t/monitor-error-when-joining-threads/144131 "2008-07-25T17:50:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![roxah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/54ee81/32.png) [@roxah](https://www.ruby-forum.com/u/roxah)\
**Post date:** [July 25, 2008, 5:50pm UTC](https://www.ruby-forum.com/t/monitor-error-when-joining-threads/144131/1 "2008-07-25T17:50:07Z")

</div>

Hello all,

_Note: this may get published twice… I posted via the email list but  
it never went through…_

I’m getting the following error when I I try to join some threads:

c:/ruby/lib/ruby/1.8/monitor.rb:215:in `mon_enter': undefined method`+’  
for nil:NilClass (NoMethodError)  
from simulator2.rb:48:in `join' from simulator2.rb:48:in`init’  
from simulator2.rb:48:in `each' from simulator2.rb:48:in`init’  
from simulator2.rb:163

For some reason @mon\_count within monitor.rb is nil, but I’m not sure  
why any type of synchronization is being done at this point. Does  
Thread do some synchronization when join is called?

Below is the code from simulator2.rb on line 48 (along with other  
potentially informative code):

threads = Array.new  
outages.each do |range|  
@logger.debug “New thread – Start: #{range.first}, Finish:  
#{range.last}”  
threads \<\< Thread.new do  
Simulator2.new(@file, @interface,  
“c:\log-#{threads.length}.txt”).run(range.first, range.last)  
end  
end  
threads.each { |thread| thread.join }

I am doing some synchronization of methods and variables within the  
Simulator2#run method… but would this show up as coming from the  
Thread#join method?

Just for clarification, yes I am creating a new instance of Simulator2  
(which is the class which this code resides in). The code above resides  
in Simulator2#init.

–  
Thanks!  
Bryan

---

<div class="post-metadata">

**Author:** ![roxah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/54ee81/32.png) [@roxah](https://www.ruby-forum.com/u/roxah)\
**Post date:** [July 25, 2008, 6:28pm UTC](https://www.ruby-forum.com/t/monitor-error-when-joining-threads/144131/2 "2008-07-25T18:28:50Z")

</div>

All,

So this doesn’t have anything to do with Thread.join and such… it  
instead has to do with some synchronization I’m doing in another class.  
Here’s what it looks like:

class StatsTracker \< Monitor  
include Singleton  
…  
def foo  
synchronize do  
# do something  
end  
end  
…  
end

I get an error at the ‘synchronize do’ line similar to what I describe  
in my first post, essentially saying @mon\_count is nil. I tried  
including MonitorMixin vs extending Monitor, but that didn’t fix the  
problem. I then tried doing the following and it worked:

class StatsTracker  
include Singleton  
def initialize  
@semaphore = Mutex.new  
end  
…  
def foo  
@semaphore.synchronize do  
# do something  
end  
end  
…  
end

Does anyone know what I’m doing wrong to make the Monitor approach not  
work?

–  
Thanks!  
Bryan

---

<div class="post-metadata">

**Author:** ![roxah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/54ee81/32.png) [@roxah](https://www.ruby-forum.com/u/roxah)\
**Post date:** [May 14, 2009, 11:57am UTC](https://www.ruby-forum.com/t/monitor-error-when-joining-threads/144131/3 "2009-05-14T11:57:54Z")

</div>

I realize this is an old question, but I’m replying in case anyone else  
encounters this error, since I just found the fix to it.

You need to make a call to super() in your constructor.

So, the following:

> class StatsTracker \< Monitor  
> include Singleton  
> …  
> def foo  
> synchronize do  
> # do something  
> end  
> end  
> …  
> end

should be changed to:

class StatsTracker \< Monitor  
include Singleton

def initialize  
super()  
end

def foo  
synchronize do  
# do something  
end  
end  
…  
end

The same is true if you’re using MonitorMixin. For instance,

class StatsTracker  
include Singleton  
include MonitorMixin

def initialize  
super()  
end

def foo  
synchronize do  
# do something  
end  
end  
…  
end

Hope that helps someone.

---

<div class="post-metadata">

**Author:** ![roxah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/54ee81/32.png) [@roxah](https://www.ruby-forum.com/u/roxah)\
**Post date:** [May 14, 2009, 1:38pm UTC](https://www.ruby-forum.com/t/monitor-error-when-joining-threads/144131/4 "2009-05-14T13:38:19Z")

</div>

2009/5/14 Jeff S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > def foo  
> > include Singleton  
> > …  
> > end
> 
> def foo  
> synchronize do  
> # do something  
> end  
> end  
> …  
> end
> 
> Hope that helps someone.

Just an additional note: the reason why the exception appears to come  
out of Thread.join is this:

irb(main):004:0\> t = Thread.new { raise ArgumentError, “test” }  
=\> #\<Thread:0x100ea960 run\>  
irb(main):005:0\> t.join  
ArgumentError: test  
from (irb):4:in `block in irb\_binding’  
irb(main):006:0\>

In other words: since the exception is not caught in the thread,  
thread exits but the exception survives. When you do the join, it  
will come to haunt you. This does make sense when you consider  
Thread.value:

irb(main):006:0\> t = Thread.new { “result” }  
=\> #\<Thread:0x100e0b54 run\>  
irb(main):007:0\> t.value  
=\> “result”  
irb(main):008:0\> t = Thread.new { raise ArgumentError, “test” }  
=\> #\<Thread:0x10016aac run\>  
irb(main):009:0\> t.value  
ArgumentError: test  
from (irb):8:in `block in irb\_binding’  
irb(main):010:0\>

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![roxah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/54ee81/32.png) [@roxah](https://www.ruby-forum.com/u/roxah)\
**Post date:** [October 12, 2010, 7:36pm UTC](https://www.ruby-forum.com/t/monitor-error-when-joining-threads/144131/5 "2010-10-12T19:36:27Z")

</div>

Jeff S. wrote in post #816096:

> I realize this is an old question, but I’m replying in case anyone else  
> encounters this error, since I just found the fix to it.
> 
> You need to make a call to super() in your constructor.

Jeff, Thanks for taking the time to record the solution. I really  
appreciated it.
