# Monitors on data structures

**URL:** <https://www.ruby-forum.com/t/monitors-on-data-structures/176470>\
**Category:** Ruby\
**Created:** [October 1, 2009, 1:22am UTC](https://www.ruby-forum.com/t/monitors-on-data-structures/176470 "2009-10-01T01:22:24Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![russellf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7bcc69/32.png) [@russellf](https://www.ruby-forum.com/u/russellf)\
**Post date:** [October 1, 2009, 1:22am UTC](https://www.ruby-forum.com/t/monitors-on-data-structures/176470/1 "2009-10-01T01:22:24Z")

</div>

Hi

I am trying to use monitors for the first time in ruby. I am familiar  
with the concept but am having some problems making things work.

Background: I have a program that uses nmap-parser to parse large nmap  
scans and uses the call back to deliver results for each host. The call  
back is threaded.

I want to build lists of IP addresses for further investigation from the  
callback routine. I have made a class AddressList:

class AddressList \< Array  
include MonitorMixin

def initialise # redundant ?  
super  
end

def \<\<(addr)  
synchronise do # make sure we don’t clobber another thread  
super( addr )  
end  
end

end

we need to protect the push code so that to threads don’t collide so I  
put a synchronise block around it.

What appears to happen is that the code never exits the synchronise  
block.

puts before the block get executed but ones after do not and nothing is  
ever added to the list.

Clearly I’m missing something.

The callback routine:

```
callback = proc do |host|

  return unless host.status == 'up'
  host.scripts do |script|
    s = script.output.gsub(/\n/, ':')
    next if s =~ / 2008 R2|ERROR/;
    next unless s =~ /Vista|2008/
    $addresses << host.addr
  end # scripts
  schedule if $addresses.size > BATCH_SIZE
end

```

yes, I’m looking for machines running smb2 to get them before the worm  
does 🙂

BTW I do have this program working without treads but I want to know why  
this does not work !

Russell

---

<div class="post-metadata">

**Author:** ![russellf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7bcc69/32.png) [@russellf](https://www.ruby-forum.com/u/russellf)\
**Post date:** [October 1, 2009, 4:09pm UTC](https://www.ruby-forum.com/t/monitors-on-data-structures/176470/2 "2009-10-01T16:09:30Z")

</div>

On Thu, 01 Oct 2009 08:22:26 +0900, Russell F. wrote:

> callback routine. I have made a class AddressList:  
> synchronise do # make sure we don’t clobber another thread  
> block.  
> return unless host.status == ‘up’  
> does 🙂
> 
> BTW I do have this program working without treads but I want to know why  
> this does not work !
> 
> Russell

synchronize is spelled with a z. You misspelled it in the example you  
posted here. Is it possible that you misspelled it in your actual code,  
and the NoMethodError is being raised in a place where it doesn’t  
generate output to alert you to the problem?

---

<div class="post-metadata">

**Author:** ![russellf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7bcc69/32.png) [@russellf](https://www.ruby-forum.com/u/russellf)\
**Post date:** [October 2, 2009, 12:49am UTC](https://www.ruby-forum.com/t/monitors-on-data-structures/176470/3 "2009-10-02T00:49:18Z")

</div>

Ken B. wrote:

> On Thu, 01 Oct 2009 08:22:26 +0900, Russell F. wrote:
> 
> > callback routine. I have made a class AddressList:  
> > synchronise do # make sure we don’t clobber another thread  
> > block.  
> > return unless host.status == ‘up’  
> > does 🙂
> > 
> > BTW I do have this program working without treads but I want to know why  
> > this does not work !
> > 
> > Russell
> 
> synchronize is spelled with a z. You misspelled it in the example you  
> posted here. Is it possible that you misspelled it in your actual code,  
> and the NoMethodError is being raised in a place where it doesn’t  
> generate output to alert you to the problem?

Of course. American spelling, sigh…

Yup! that fixed it! Thanks very much Ken

Need a “pragma: Ex British Empire” 🙂 🙂

I wonder why the NoMethodError does not appear?

---

<div class="post-metadata">

**Author:** ![russellf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7bcc69/32.png) [@russellf](https://www.ruby-forum.com/u/russellf)\
**Post date:** [October 2, 2009, 5:09am UTC](https://www.ruby-forum.com/t/monitors-on-data-structures/176470/4 "2009-10-02T05:09:17Z")

</div>

On Fri, 02 Oct 2009 07:49:40 +0900, Russell F. wrote:

> > > why this does not work !
> 
> Yup! that fixed it! Thanks very much Ken
> 
> Need a “pragma: Ex British Empire” 🙂 🙂
> 
> I wonder why the NoMethodError does not appear?

I guess threads just supress them in general.

irb(main):001:0\> Thread.start{raise NoMethodError}  
=\> #\<Thread:0x7fe5ee8ce930 dead\>  
irb(main):002:0\>

---

<div class="post-metadata">

**Author:** ![russellf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7bcc69/32.png) [@russellf](https://www.ruby-forum.com/u/russellf)\
**Post date:** [October 2, 2009, 10:26am UTC](https://www.ruby-forum.com/t/monitors-on-data-structures/176470/5 "2009-10-02T10:26:34Z")

</div>

On 2 oct. 2009, at 05:08, Ken B. wrote:

> I guess threads just supress them in general.
> 
> irb(main):001:0\> Thread.start{raise NoMethodError}  
> =\> #\<Thread:0x7fe5ee8ce930 dead\>  
> irb(main):002:0\>

irb(main):001:0\> Thread.start { raise NoMethodException }  
=\> #\<Thread:0x100577848 dead\>

irb(main):002:0\> Thread.abort\_on\_exception = true  
=\> true

irb(main):003:0\> Thread.start { raise NoMethodException }  
(irb):3:in `irb_binding': uninitialized constant NoMethodException (NameError) from (irb):3:in`start’  
from (irb):3:in `irb_binding' from /opt/local/lib/ruby/1.8/irb/workspace.rb:52:in`irb\_binding’  
from /opt/local/lib/ruby/1.8/irb/workspace.rb:52

---

<div class="post-metadata">

**Author:** ![russellf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7bcc69/32.png) [@russellf](https://www.ruby-forum.com/u/russellf)\
**Post date:** [October 2, 2009, 12:20pm UTC](https://www.ruby-forum.com/t/monitors-on-data-structures/176470/6 "2009-10-02T12:20:44Z")

</div>

On 2 oct. 2009, at 10:25, Luc H. wrote:

> (irb):3:in `irb\_binding’: uninitialized constant NoMethodException  
> (NameError)

Interesting, I somewhat managed to screw up the name of the exception,  
which thankfully causes _another_ exception, still demonstrating the  
usefulness of Thread.abort\_on\_exception 🙂

---

<div class="post-metadata">

**Author:** ![russellf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7bcc69/32.png) [@russellf](https://www.ruby-forum.com/u/russellf)\
**Post date:** [October 2, 2009, 5:08pm UTC](https://www.ruby-forum.com/t/monitors-on-data-structures/176470/7 "2009-10-02T17:08:10Z")

</div>

On Fri, Oct 2, 2009 at 1:25 AM, Luc H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> =\> #\<Thread:0x100577848 dead\>  
> from /opt/local/lib/ruby/1.8/irb/workspace.rb:52
> 
> Or you can save the reference to the thread and join it:

irb(main):003:0\> t = Thread.start{raise NoMethodError}  
=\> #\<Thread:0x7f5ee476a058 dead\>  
irb(main):004:0\> t.join  
NoMethodError: NoMethodError  
from (irb):3  
from (irb):4:in `join’  
from (irb):4  
from :0
