# Help with threads

**URL:** <https://www.ruby-forum.com/t/help-with-threads/141806>\
**Category:** Ruby\
**Created:** [June 30, 2008, 6:57pm UTC](https://www.ruby-forum.com/t/help-with-threads/141806 "2008-06-30T18:57:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![fox](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/c5a1d2/32.png) [@fox](https://www.ruby-forum.com/u/fox)\
**Post date:** [June 30, 2008, 6:57pm UTC](https://www.ruby-forum.com/t/help-with-threads/141806/1 "2008-06-30T18:57:38Z")

</div>

hi all,  
i’m trying to do some automated tasks with threads.  
Basically, my app is initialized with an input file, and an output path  
and some processing is done on that.  
I’m trying to automatically detect when the thread dies, so that it can  
tell the parent class that this thread is complete.  
Is there a nice way to do this without polling every thread? I seem to  
recall something relating to the Rufus::Scheduler stuff being able to do  
similar, but i can’t remember the name of that or where i found it. any  
information would be great!

thanks,  
ol.

---

<div class="post-metadata">

**Author:** ![fox](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/c5a1d2/32.png) [@fox](https://www.ruby-forum.com/u/fox)\
**Post date:** [June 30, 2008, 11:56pm UTC](https://www.ruby-forum.com/t/help-with-threads/141806/2 "2008-06-30T23:56:23Z")

</div>

On Mon, Jun 30, 2008 at 12:54 PM, Oliver F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> hi all,  
> i’m trying to do some automated tasks with threads.  
> Basically, my app is initialized with an input file, and an output path  
> and some processing is done on that.  
> I’m trying to automatically detect when the thread dies, so that it can  
> tell the parent class that this thread is complete.  
> Is there a nice way to do this without polling every thread? I seem to  
> recall something relating to the Rufus::Scheduler stuff being able to do  
> similar, but i can’t remember the name of that or where i found it. any  
> information would be great!

Hi Oliver,

Have you looked at the Monitor class and using MonitorMixin? I  
believe your main thread could do a “wait\_while” and your child  
threads could do a “signal” when they’re done to notify the main  
thread.

This is assuming you have multiple child threads. If there’s only  
one, then doing a “join” would be easier.

I hope that helps,

Eric

====

[LearnRuby.com](http://LearnRuby.com) offers Rails & Ruby HANDS-ON public & ON-SITE workshops.  
Please visit [http://LearnRuby.com](http://LearnRuby.com) for all the details.

---

<div class="post-metadata">

**Author:** ![fox](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/c5a1d2/32.png) [@fox](https://www.ruby-forum.com/u/fox)\
**Post date:** [July 1, 2008, 12:26pm UTC](https://www.ruby-forum.com/t/help-with-threads/141806/3 "2008-07-01T12:26:33Z")

</div>

Hello Oliver,

I needed to do something similar than you once, but in my case I needed  
to  
wait until all the threads were finished.

I’ve just looped in the main thread while the Thread.list.size was  
greater  
than 1.

I hope this helps,

Guillermo

From:  
Oliver F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To:  
removed\_email\_address@domain.invalid (ruby-talk ML)  
Date:  
30.06.2008 19:00  
Subject:  
help with threads

hi all,  
i’m trying to do some automated tasks with threads.  
Basically, my app is initialized with an input file, and an output path  
and some processing is done on that.  
I’m trying to automatically detect when the thread dies, so that it can  
tell the parent class that this thread is complete.  
Is there a nice way to do this without polling every thread? I seem to  
recall something relating to the Rufus::Scheduler stuff being able to do  
similar, but i can’t remember the name of that or where i found it. any  
information would be great!

thanks,  
ol.

---

<div class="post-metadata">

**Author:** ![fox](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/c5a1d2/32.png) [@fox](https://www.ruby-forum.com/u/fox)\
**Post date:** [July 1, 2008, 3:09pm UTC](https://www.ruby-forum.com/t/help-with-threads/141806/4 "2008-07-01T15:09:46Z")

</div>

Oliver F. wrote:

> hi all,  
> i’m trying to do some automated tasks with threads.  
> Basically, my app is initialized with an input file, and an output path  
> and some processing is done on that.  
> I’m trying to automatically detect when the thread dies, so that it can  
> tell the parent class that this thread is complete.  
> Is there a nice way to do this without polling every thread? I seem to  
> recall something relating to the Rufus::Scheduler stuff being able to do  
> similar, but i can’t remember the name of that or where i found it. any  
> information would be great!

A thread won’t die without reason. If the thread completes normally, you  
can set a flag or set the thread’s result to something non-nil  
(Thread.current[:some\_key] = result) that you can check for. If it  
terminates early due to an exception, you can either rescue that  
exception or let it bubble out and terminate the thread, which will set  
the thread’s value to the exception object.

To avoid constantly polling, you want some kind of synchronization  
mechanism like those in the ‘thread’ library (Monitors, Semaphores,  
etc).

- Charlie
