Removing a dead thread

Scenario: I have a set of 20 things to do. I can only do 4 things at a
time.

How can I limit myself to 4 threads?

This is what I have:

threads = []
designs.each { |d|
while threads.size > ARGV[0].to_i
do something?
end
threads << Thread.new(d) { |t|
do stuff…
}
}

how would I remove a thread once it’s done?

ideas?

db

···


Jan 31 Jackie Robinson born, 1919
Jan 31 Hewlett-Packard founded, 1939
Jan 31 “Ham” the chimpanzee soars into space aboard Mercury-Redstone 2, 1961
Jan 31 Explorer I launched, 1958. Van Allen Belt discovered
Jan 31 Irving Langmuir, 1881, invented tungsten filament lamp
Feb 1 First TV soap: Secret Storm, 1954
Feb 1 Forces led by Khomeini take over Iran, 1979
Feb 1 Chinese New Year Holiday (3 days) in Taiwan
Feb 2 Candlemas
Jan 31 The Grateful Dead are busted in New Orleans, 1970
Feb 1 RCA Victor unveils the 45 rpm record playing system, 1949
Feb 2 Graham Nash is born in Lancashire, England, 1942
Feb 2 Groundhog Day
Feb 2* Parashat Yitro

does this seem reasonable?

while threads.size > ARGV[0].to_i
threads.each { |aThread| threads.delete(aThread) unless aThread.alive? }
end

···

On Fri, Jan 31, 2003 at 06:59:05PM -0500, Daniel Bretoi wrote:

Scenario: I have a set of 20 things to do. I can only do 4 things at a
time.

How can I limit myself to 4 threads?

This is what I have:

threads =
designs.each { |d|
while threads.size > ARGV[0].to_i
do something?
end
threads << Thread.new(d) { |t|
do stuff…
}
}

how would I remove a thread once it’s done?

ideas?

db


Jan 31 Jackie Robinson born, 1919
Jan 31 Hewlett-Packard founded, 1939
Jan 31 “Ham” the chimpanzee soars into space aboard Mercury-Redstone 2, 1961
Jan 31 Explorer I launched, 1958. Van Allen Belt discovered
Jan 31 Irving Langmuir, 1881, invented tungsten filament lamp
Feb 1 First TV soap: Secret Storm, 1954
Feb 1 Forces led by Khomeini take over Iran, 1979
Feb 1 Chinese New Year Holiday (3 days) in Taiwan
Feb 2 Candlemas
Jan 31 The Grateful Dead are busted in New Orleans, 1970
Feb 1 RCA Victor unveils the 45 rpm record playing system, 1949
Feb 2 Graham Nash is born in Lancashire, England, 1942
Feb 2 Groundhog Day
Feb 2* Parashat Yitro


Jan 31 Jackie Robinson born, 1919
Jan 31 Hewlett-Packard founded, 1939
Jan 31 “Ham” the chimpanzee soars into space aboard Mercury-Redstone 2, 1961
Jan 31 Explorer I launched, 1958. Van Allen Belt discovered
Jan 31 Irving Langmuir, 1881, invented tungsten filament lamp
Feb 1 First TV soap: Secret Storm, 1954
Feb 1 Forces led by Khomeini take over Iran, 1979
Feb 1 Chinese New Year Holiday (3 days) in Taiwan
Feb 2 Candlemas
Jan 31 The Grateful Dead are busted in New Orleans, 1970
Feb 1 RCA Victor unveils the 45 rpm record playing system, 1949
Feb 2 Graham Nash is born in Lancashire, England, 1942
Feb 2 Groundhog Day
Feb 2* Parashat Yitro

This might seem overly complicated but it works, pools your
connections/handlers/whatever and is general enough to be reused with
little change. (Don’t be scared, look at the bottom and see how easy it
is to use :slight_smile:

batsman@tux-chan:/tmp$ expand -t 2 c.rb
require ‘thread’

class PooledWhatever
def initialize(klass, howmany, args, debug = false)
@mutex = Mutex.new
@pool =
@handleravail = ConditionVariable.new
howmany.times { @pool << klass.new(*args) }
@threads = {}
puts “Handler pool: #{@pool.inspect}” if debug
end

def run(debug = false)
ref =
handler = nil
@mutex.synchronize do
info = false
if @pool.size == 0
@handleravail.wait @mutex
info = true if debug
end
handler = @pool.shift
puts “Got handler #{handler} after getting blocked.” if info
@threads[handler] = ref # hack cause want this
# inside synchronize; perhaps not needed
end
ref << Thread.new do
begin
yield handler
ensure
@mutex.synchronize do
@pool << handler
@threads.delete handler
@handleravail.signal
end
end
end
end

def wait_for_all
@threads.each do |handler, thread|
thread[0].join
end
@pool.each { |x| x.close }
end

def num_tasks
@threads.size
end
end

class Dummy
def close
puts “Closing handler #{self}…”
end
end

runner = PooledWhatever.new(Dummy, 4, , true)
10.times do |i|
runner.run(true) do
puts “DOING MAGIC: num threads == #{runner.num_tasks}”
sleep ( i.to_f / 20 )
end
end
runner.wait_for_all

batsman@tux-chan:/tmp$ ruby c.rb
Handler pool: [#Dummy:0x40247a20, #Dummy:0x40247a0c,
#Dummy:0x402479bc, #Dummy:0x402479a8]
DOING MAGIC: num threads == 1
DOING MAGIC: num threads == 2
DOING MAGIC: num threads == 2
DOING MAGIC: num threads == 3
DOING MAGIC: num threads == 4
Got handler #Dummy:0x40247a0c after getting blocked.
DOING MAGIC: num threads == 4
Got handler #Dummy:0x402479bc after getting blocked.
DOING MAGIC: num threads == 4
Got handler #Dummy:0x402479a8 after getting blocked.
DOING MAGIC: num threads == 4
Got handler #Dummy:0x40247a20 after getting blocked.
DOING MAGIC: num threads == 4
Got handler #Dummy:0x40247a0c after getting blocked.
DOING MAGIC: num threads == 4
Closing handler #Dummy:0x402479bc
Closing handler #Dummy:0x402479a8
Closing handler #Dummy:0x40247a20
Closing handler #Dummy:0x40247a0c

···

On Sat, Feb 01, 2003 at 08:35:24AM +0900, Daniel Bretoi wrote:

Scenario: I have a set of 20 things to do. I can only do 4 things at a
time.

How can I limit myself to 4 threads?

This is what I have:

threads =
designs.each { |d|
while threads.size > ARGV[0].to_i
do something?
end
threads << Thread.new(d) { |t|
do stuff…
}
}

how would I remove a thread once it’s done?

ideas?


_ _

__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

The only “intuitive” interface is the nipple. After that, it’s all learned.
– Bruce Ediger, bediger@teal.csn.org, on X interfaces

More info at

···

On Sat, Feb 01, 2003 at 08:35:24AM +0900, Daniel Bretoi wrote:

Scenario: I have a set of 20 things to do. I can only do 4 things at a
time.

How can I limit myself to 4 threads?


_ _

__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

Remember: While root can do most everything, there are certain
privileges that only a partner can grant.
– Telsa Gwynne