# Ruby, Curses, Threading, and Sleeping

**URL:** <https://www.ruby-forum.com/t/ruby-curses-threading-and-sleeping/86282>\
**Category:** Ruby\
**Created:** [February 21, 2007, 3:45am UTC](https://www.ruby-forum.com/t/ruby-curses-threading-and-sleeping/86282 "2007-02-21T03:45:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![badcherry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c37758/32.png) [@badcherry](https://www.ruby-forum.com/u/badcherry)\
**Post date:** [February 21, 2007, 3:45am UTC](https://www.ruby-forum.com/t/ruby-curses-threading-and-sleeping/86282/1 "2007-02-21T03:45:41Z")

</div>

I’m attempting to get a curses application running in a threaded  
environment to handle input with no delay. This is needed for  
portability reasons. I have found though that in a threaded ruby  
environment that whenever there is a call to sleep (even in a  
different thread) then Curses behaves differently. The following code  
acts as I expect it to:

require ‘curses’  
require ‘pp’

ch = []

Curses::init\_screen  
Curses::stdscr.nodelay = true

t = Thread.new do  
x = 0  
end

while c = Curses::stdscr.getch  
break if c == 9 #tab exits  
sleep 0.1  
Curses::setpos(10,10)  
Curses::addstr(" ")  
Curses::addstr(c.to\_s)  
Curses::refresh  
ch \<\< [c]  
end

Curses::close\_screen  
pp ch

It works just fine and grabs input with out waiting for it and returns  
an error value when there is no input. If you change the line ‘x = 0’  
to ‘sleep 1’ then the behavior of Curses changes and the line c =  
Curses::stdscr.getch will now sit idle until a key has been pressed.

Is there a better way of making threads idle other than sleep that  
won’t cause this side effect? Is there a better way of grabbing input  
that the way that I am using?

## My stats: Emachines M6805 (running in 32bit) Ubuntu 6.10 Ruby 1.8.4

Barry Dmytro  
removed\_email\_address@domain.invalid  
[http://badcherry.org/](http://badcherry.org/)

---

<div class="post-metadata">

**Author:** ![badcherry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c37758/32.png) [@badcherry](https://www.ruby-forum.com/u/badcherry)\
**Post date:** [February 21, 2007, 1:00pm UTC](https://www.ruby-forum.com/t/ruby-curses-threading-and-sleeping/86282/2 "2007-02-21T13:00:27Z")

</div>

badcherry [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m attempting to get a curses application running in a threaded

curses isn’t thread-safe.

---

<div class="post-metadata">

**Author:** ![badcherry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c37758/32.png) [@badcherry](https://www.ruby-forum.com/u/badcherry)\
**Post date:** [February 22, 2007, 6:25am UTC](https://www.ruby-forum.com/t/ruby-curses-threading-and-sleeping/86282/3 "2007-02-22T06:25:35Z")

</div>

Well yeah, I realize that, but anyone have any suggestions for making  
this work? It runs just fine on \*nix systems done a different way,  
but will then not run in windows do to how ruby threads behave in  
windows. I want this to run on both platforms.
