require 'tools.rb' class Array # run each in parallel (WARNING: creates infinite threads !!!) def p_each(&block) threads=[] self.each{|element| thread = Thread.new(element,block){|element,block| block.call(element) } threads << thread } (threads.uniq-[Thread.current]).each{|t| assert{Thread.current!=t} t.join } threads.each{|t|t.kill!} end # run map in parallel (WARNING: creates infinite threads !!!) def p_map(&block) threads=[] results=[nil]*length mutex=Mutex.new i=0 self.each{|element| t=Thread.start(element,block,results,mutex,i){|element,block,results,mutex,mi| j=mi begin res=block.call(element) mutex.synchronize { results[j]=res } rescue Object=>e pp e,e.stacktrace end } threads<