2011-02-18 17 views

回答

26

由于可能会从行中单独写入新行,因此存在竞争条件。您可以在多线程应用程序中使用看跌期权看到这种噪音:

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 <em>ios</em> as with 
* <code>IO#print</code>. 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") 
* 
* <em>produces:</em> 
* 
*  this 
*  is 
*  a 
*  test 
*/ 
+0

哇,我刚刚在我的一些日志文件中与此生活在一起。看起来像一个简单的修复。 – 2011-02-18 18:22:03