Charles Oliver Nutter
7/1/2008 1:06:00 PM
Oliver Fox 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