Project

General

Profile

Actions

Feature #5788

closed

Thread#at_exit

Added by Glass_saga (Masaki Matsushita) over 12 years ago. Updated almost 6 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
[ruby-core:41774]

Description

=begin
I propose a new method Thread#at_exit.
It is to register a block which will be called when the thread ends.

p Thread.main #=> #<Thread:0x007f828fa97fe0 run>
p t = Thread.new{ # do something } #=> #<Thread:0x007f828fc9e500 run>
t.at_exit{ p Thread.current }
t.join #=> #<Thread:0x007f828fc9e500 run>

Thread.main.at_exit is equal to Kernel.at_exit.

I think this method is useful for waiting plural running threads end.

Without Thread#at_exit:
t1 = Thread.new{ sleep 1 }
t2 = Thread.new{ sleep 2 }

[t1, t2].each do |t|
Thread.new do
t.join
puts "Thread ended!"
end
end

[t1, t2].each(&:join)

To handle threads' end immediately, the same number of threads as ones to wait is needed.

For example, Ruby's standard library thwait.rb does so.

With Thread#at_exit:
t1 = Thread.new{ sleep 1 }
t2 = Thread.new{ sleep 2 }

[t1, t2].each do |t|

It runs when interpreter ends.

t.at_exit{ puts "Thread ended!" }
end

New threads are not necessary.

I made a patch.
Patched ruby passes test-all.
=end


Files

patch.diff (2.13 KB) patch.diff Glass_saga (Masaki Matsushita), 12/22/2011 05:35 PM
patch2.diff (2.41 KB) patch2.diff Glass_saga (Masaki Matsushita), 12/25/2011 12:42 AM
patch3.diff (3.26 KB) patch3.diff Glass_saga (Masaki Matsushita), 02/01/2012 10:58 PM
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0