title: Writing to a file from multiple threads
body: <p>I am trying to write out to a text file using multiple threads, however the code below gives me an exception - <code>IOError: closed stream</code></p>
  <pre class="lang-ruby prettyprint-override"><code>threads = []
  File.open(&quot;test.txt&quot;, &quot;a&quot;) do |fp|
    500.times do |time|
      threads &lt;&lt; Thread.new do
        fp.puts(&quot;#{time}: 1&quot;)
        sleep(rand(100) / 100.0)
        fp.puts(&quot;#{time}: 2&quot;)
      end
    end
  end
  
  threads.each{ |thread| thread.join }
  </code></pre>