在没有混淆输出的情况下,在Ruby中从并行操作打印输出的最简单方法是什么?

假设我分叉了一堆线程,并希望将每个线程的进度输出打印到STDERR。 我怎样才能确保输出保持行primefaces性,即不会混淆同一输出行中不同线程的输出?

# run this a few times and you'll see the problem threads = [] 10.times do threads << Thread.new do puts "hello" * 40 end end threads.each {|t| t.join} 

puts有一个竞争条件,因为它可能会从行中单独写入新行。 您可以在multithreading应用程序中使用puts来看到这种噪音:

 thread 0thread 1 thread 0thread 2 thread 1 thread 0thread 3 thread 2 thread 1 

相反,使用print或printf

 print "thread #{i}" + "\n" print "thread #{i}\n" printf "thread %d\n", i 

或者,因为您要写入STDERR:

 $stderr.print "thread #{i}\n" 

这是Ruby中的错误吗? 如果评论被视为标准,则不是。 这是从MRI 1.8.7到2.2.2的IO.puts的定义:

 /* * call-seq: * ios.puts(obj, ...) => nil * * Writes the given objects to ios as with * IO#print. Writes a record separator (typically a * newline) after any that do not already end with a newline sequence. * If called with an array argument, writes each element on a new line. * If called without arguments, outputs a single record separator. * * $stdout.puts("this", "is", "a", "test") * * produces: * * this * is * a * test */