Forum: Ruby Unexpected output with the Curses.getch function call

941587f86c2f65a08dec808da1e3c1ce?d=identicon&s=25 Nikita Trophimov (Guest)
on 2014-08-18 23:21
(Received via mailing list)
Greetings!

The following code terminates the application right after the
Kernel#exit function call:

first = Thread.new do
   loop {
     gets.chomp
   }
end

second = Thread.new do
   Kernel::exit
end

second.join
first.join

However, if I replace the gets.chomp with the Curses.getch function
call, I've got an unexpected result -- the application will wait for the
next user input and terminates only after it.

Why? Can I somehow fix this behavior?

OS -- Windows 8.

ruby 2.0.0p481 (2014-05-08) [x64-mingw32]

Thanks in advance.


---
This email is free from viruses and malware because avast! Antivirus
protection is active.
http://www.avast.com
09a32175057418748822c587ac08c429?d=identicon&s=25 Abinoam Jr. (abinoampraxedes_m)
on 2014-08-19 13:11
(Received via mailing list)
Hi Nikita Trophimov,

Perhaps it's a time slicing issue.
I think gets is not blocking the thread and Kernel.exit in its own
time slice puts everything down.

With Curses.getch I think the thread gets blocked waiting for input.

Instructing the other thread to sleep makes the chomp work.

require 'curses'

first = Thread.new do
  loop {
    ch = gets.chomp
    puts "You have typed #{ch}"
  }
end

second = Thread.new do
  sleep 5.0 # <--- Sleep for 5 seconds "passing" to the other threads.
  Kernel::exit
end

second.join
first.join

Try to block the first thread in another way.
(12345 ** 10 ** 6).prime?
The operation above takes some seconds on my machine.
So I see the same effect as Curses.getch.

first = Thread.new do
  loop {
    if (12345 ** 10 ** 6).prime?
      puts "It's prime"
    else
      puts "It's not prime"
    end
  }
end

second = Thread.new do
  Kernel::exit
end

second.join
first.join

See: http://merbist.com/2011/02/22/concurrency-in-ruby-explained/

Abinoam Jr.

On Mon, Aug 18, 2014 at 6:21 PM, Nikita Trophimov
941587f86c2f65a08dec808da1e3c1ce?d=identicon&s=25 Nikita Trophimov (Guest)
on 2014-08-19 14:16
(Received via mailing list)
In both cases I see the same result as in my first example with
gets.chomp -- the whole application was shutdown without any key press.
Is there difference between our environment? Maybe OS or Ruby version?

On 8/19/2014 3:11 PM, Abinoam Jr. wrote:
> require 'curses'
>    Kernel::exit
> first = Thread.new do
>    Kernel::exit
> <nikita.trophimov@gmail.com> wrote:
>>
>>
>> This email is free from viruses and malware because avast! Antivirus
>> protection is active.
>> http://www.avast.com
>>


---
This email is free from viruses and malware because avast! Antivirus
protection is active.
http://www.avast.com
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (robert_k78)
on 2014-08-19 17:53
(Received via mailing list)
On Mon, Aug 18, 2014 at 11:21 PM, Nikita Trophimov
<nikita.trophimov@gmail.com> wrote:
>
>
> Why? Can I somehow fix this behavior?

I am suspecting threading. Code in Curses suggests that there are
threads involved whereas Kernel.gets will just do plain IO.
http://www.ruby-doc.org/stdlib-2.0.0/libdoc/curses...
(click on source view)

Kind regards

robert
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.