Unexpected output with the Curses.getch function call

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

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: Merbist

Abinoam Jr.

···

On Mon, Aug 18, 2014 at 6:21 PM, Nikita Trophimov <nikita.trophimov@gmail.com> wrote:

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

I am suspecting threading. Code in Curses suggests that there are
threads involved whereas Kernel.gets will just do plain IO.

(click on source view)

Kind regards

robert

···

On Mon, Aug 18, 2014 at 11:21 PM, Nikita Trophimov <nikita.trophimov@gmail.com> wrote:

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?

--
[guy, jim].each {|him| remember.him do |as, often| as.you_can - without end}
http://blog.rubybestpractices.com/

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:

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 > <nikita.trophimov@gmail.com> wrote:

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

---
This email is free from viruses and malware because avast! Antivirus protection is active.