Skip to content

Instantly share code, notes, and snippets.

@fzero
Last active July 30, 2018 18:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fzero/d9b777e60e3cd2c008f5a062ced55b49 to your computer and use it in GitHub Desktop.
Save fzero/d9b777e60e3cd2c008f5a062ced55b49 to your computer and use it in GitHub Desktop.
Ruby skeleton for parallel iteration using threads with thread count
# 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