ruby-thread/process

thread

a = 1
threads = []
mutex = Mutex.new

5.times do
  threads << Thread.new do
    1000.times do
      mutex.synchronize{
        a = a + 1
      }
    end
  end
end

threads.each{ |t| t.exit }  # t.kill
threads.each{ |t| t.join(10) }
puts a

 

process基于unix

process = []

5.times do
	process << Process.fork do
		puts "Chile do hard work. #{Process.pid}:#{Process.ppid}"
		sleep 10
	end
end

#puts "I have child"
process.each{ |pid| Process.wait(pid)}

 

posted on 2016-09-20 09:18  lly001  阅读(172)  评论(0编辑  收藏  举报

导航