From: Michael Neumann Date: 2000-07-14T17:59:14+02:00 Subject: [ruby-talk:04018] Re: Thread and File do not work together From: "Dave Thomas" > "Michael Neumann" writes: > > > It seems to me that I cannot read from files inside a thread. If I do so, > > the thread terminates. > > Does the following code work for you (run it with the number of > threads to start as an argument) > > threads = [] > 1.upto(ARGV[0].to_i) do |num| > puts "In thread #{num}" > thread = Thread.start do > myNumber = num > IO.foreach("/etc/fstab") do |line| > puts "#{myNumber}: #{line}" > Thread.pass > end > end > threads << thread > puts "Leaving thread #{num}" > end > > threads.each { |t| t.join } > This works. But in the following example the programm hangs after printing out some lines (~80 on my system). To try it, first start this script, then execute "telnet 127.0.0.1 1000". Could it be, that "gs.accept" (1) blocks the output (2)? require "socket" gs = TCPServer.open(1000) threads = [] Thread.abort_on_exception = true while true ns = gs.accept # (1) ns.close thread = Thread.start do line_nr = 0 IO.foreach("test.txt") do |line| puts "#{line_nr}: #{line}" # (2) line_nr += 1 end end threads << thread end > If so, then you may just have a simple bug in your code. Try setting > > Thread.abort_on_exception = true > > and running your code. > > > Regards > > > Dave Michael