[#3986] Re: Principle of least effort -- another Ruby virtue. — Andrew Hunt <andy@...>

> Principle of Least Effort.

14 messages 2000/07/14

[#4043] What are you using Ruby for? — Dave Thomas <Dave@...>

16 messages 2000/07/16

[#4139] Facilitating Ruby self-propagation with the rig-it autopolymorph application. — Conrad Schneiker <schneik@...>

Hi,

11 messages 2000/07/20

[ruby-talk:04018] Re: Thread and File do not work together

From: "Michael Neumann" <neumann@...>
Date: 2000-07-14 15:59:14 UTC
List: ruby-talk #4018
From: "Dave Thomas" <Dave@Thomases.com>
> "Michael Neumann" <neumann@s-direktnet.de> 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


In This Thread