M
Mark
Hi,
I was trying to do a little exercise: Display series of numbers
(1,2,3,4, 5....etc) in an infinite loop. The program should quit if
someone hits a specific key (ENTER in my case as the escape key
requires ugly solutions for non-buffered input). Sounds simple, right?
Not. I thought this should work:
$done = FALSE
Thread.new do
STDIN.getc
$done = TRUE
end
i = 0
while !$done
i = i + 1
puts i
end
puts "We're done here..."
It doesn't work. What I see is the loop running until i equals 60 then
the loop stops! When I press ENTER, the program stops (as expected).
If I replace STDIN.getc by a sleep(3) then the loop runs for 3 seconds
as expected and the program terminates.
My conclusion: using puts and getc in different threads causes
trouble. Why is this? How can this be fixed? Should I go about the
'loop until key pressed'-problem' in an entirely different way?
Thanx!
Mark
I was trying to do a little exercise: Display series of numbers
(1,2,3,4, 5....etc) in an infinite loop. The program should quit if
someone hits a specific key (ENTER in my case as the escape key
requires ugly solutions for non-buffered input). Sounds simple, right?
Not. I thought this should work:
$done = FALSE
Thread.new do
STDIN.getc
$done = TRUE
end
i = 0
while !$done
i = i + 1
puts i
end
puts "We're done here..."
It doesn't work. What I see is the loop running until i equals 60 then
the loop stops! When I press ENTER, the program stops (as expected).
If I replace STDIN.getc by a sleep(3) then the loop runs for 3 seconds
as expected and the program terminates.
My conclusion: using puts and getc in different threads causes
trouble. Why is this? How can this be fixed? Should I go about the
'loop until key pressed'-problem' in an entirely different way?
Thanx!
Mark