Working with thread

How can I ensure that the main child lives till all children finishes
their jobs and get killed?

Is there any way that I can invoke a function when a thread gets killed?

2012/8/10 ajay paswan [email protected]:

How can I ensure that the main child lives till all children finishes
their jobs and get killed?

Thread#join?

Is there any way that I can invoke a function when a thread gets killed?

I don’t understand. Are you looking for #at_exit?

– Matma R.

On Fri, Aug 10, 2012 at 2:47 PM, Bartosz Dziewoński
[email protected] wrote:

2012/8/10 ajay paswan [email protected]:

How can I ensure that the main child lives till all children finishes
their jobs and get killed?

Thread#join?

You can remove the question mark. :slight_smile:

A typical idiom looks like this

threads = 10.times.map {|i| Thread.new { … } }

threads.each &:join

alternative: collect all results

results = threads.map &:value

Is there any way that I can invoke a function when a thread gets killed?

I don’t understand. Are you looking for #at_exit?

That is invoked when the main thread exits but not per thread. Per
thread one can do

Thread.new do
begin
do_work
ensure
printf “Thread dies %p\n”, Thread.current
end
end

But using Thread#join or Thread#value is usually sufficient.

Kind regards

robert

ajay paswan wrote in post #1072149:

Sorry for the mistake… I wanted to ask, how can I ensure that the main
thread lives till all children completes their jobs.

Read the replies. This has been answered already.

Bartosz Dziewoński wrote in post #1071948:

2012/8/10 ajay paswan [email protected]:

How can I ensure that the main child lives till all children finishes
their jobs and get killed?

Sorry for the mistake… I wanted to ask, how can I ensure that the main
thread lives till all children completes their jobs.