Created
March 20, 2014 18:04
-
-
Save ranjib/9670069 to your computer and use it in GitHub Desktop.
This file contains 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
# process based .. has memory implication | |
sleep_time = 10 | |
pids= [] | |
10.times do |n| | |
pids << fork do | |
sleep sleep_time | |
end | |
sleep_time += 10 | |
end | |
# not exactly parallel,, this will halt the execution till the pid returns (im sure Process class has some async api, to get around this) | |
pids.each do |pid| | |
Process.waitpid(pid) | |
end | |
# thread based | |
sleep_time = 10 | |
threads = [] | |
10.times do |n| | |
threads << Thread.new do | |
sleep sleep_time | |
end | |
sleep_time += 10 | |
end | |
pids.each do |thread| | |
thread.join | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment