Skip to content

Commit d375f65

Browse files
committed
Removed thread pool out of main and into a seperate file. Code was based off of code foudn here: http://burgestrand.se/code/ruby-thread-pool/
1 parent ab73ab3 commit d375f65

File tree

2 files changed

+34
-30
lines changed

2 files changed

+34
-30
lines changed

generator.rb

+1-30
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,6 @@
11
require 'thread'
22
require 'socket'
3-
4-
class Pool
5-
def initialize(size)
6-
@size = size
7-
@jobs = Queue.new
8-
@pool = Array.new(@size) do |i|
9-
Thread.new do
10-
Thread.current[:id] = i
11-
catch(:exit) do
12-
loop do
13-
job, args = @jobs.pop
14-
job.call(*args)
15-
end
16-
end
17-
end
18-
end
19-
end
20-
21-
def schedule(*args, &block)
22-
@jobs << [block, args]
23-
end
24-
25-
def shutdown
26-
@size.times do
27-
schedule { throw :exit }
28-
end
29-
30-
@pool.map(&:join)
31-
end
32-
end
3+
require_relative 'thread-pool.rb'
334

345
unless ARGV.length == 2
356
puts 'Usage: ruby generator.rb host_address host_ port'

thread-pool.rb

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# This code was found http://burgestrand.se/code/ruby-thread-pool/
2+
# Decided to use this pool implementation then creating my own
3+
4+
class Pool
5+
def initialize(size)
6+
@size = size
7+
@jobs = Queue.new
8+
@pool = Array.new(@size) do |i|
9+
Thread.new do
10+
Thread.current[:id] = i
11+
catch(:exit) do
12+
loop do
13+
job, args = @jobs.pop
14+
job.call(*args)
15+
end
16+
end
17+
end
18+
end
19+
end
20+
21+
def schedule(*args, &block)
22+
@jobs << [block, args]
23+
end
24+
25+
def shutdown
26+
@size.times do
27+
schedule { throw :exit }
28+
end
29+
30+
@pool.map(&:join)
31+
end
32+
end
33+

0 commit comments

Comments
 (0)