Last active
July 30, 2018 18:27
-
-
Save fzero/d9b777e60e3cd2c008f5a062ced55b49 to your computer and use it in GitHub Desktop.
Ruby skeleton for parallel iteration using threads with thread count
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Thread count control | |
MAXTHREADS = 30 | |
# This is the collection of data you need to process with threads | |
collection = [] | |
# A nice array to store your thread objects | |
threads = [] | |
collection.each do |item| | |
# Wait before adding threads when the limit is reached | |
if threads.size >= MAXTHREADS | |
# Pull threads of our control array while they exist | |
# We're going FIFO here | |
while thread = threads.shift do | |
# Synchronize to open space for more threads | |
thread.join | |
end | |
end | |
# Now that we're sure we have available thread slots, fill them | |
# up with whatever we need to do in parallel | |
threads << Thread.new do | |
# Your parallelizable code here | |
end | |
end | |
# Final cleanup | |
if threads.any? | |
while thread = threads.shift do | |
thread.join | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment