[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Ruby, Curses, Threading, and Sleeping...

Barry Dmytro

2/21/2007 2:40:00 AM

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
badcherry@mailc.net
http://badc...

2 Answers

Thomas Dickey

2/21/2007 12:00:00 PM

0

badcherry <badcherry@mailc.net> wrote:
> I'm attempting to get a curses application running in a threaded

curses isn't thread-safe.

--
Thomas E. Dickey
http://invisible-...
ftp://invisible-...

Barry Dmytro

2/22/2007 5:21:00 AM

0


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.

On Feb 21, 5:59 am, Thomas Dickey <dic...@saltmine.radix.net> wrote:
> badcherry <badche...@mailc.net> wrote:
> > I'm attempting to get a curses application running in a threaded
>
> curses isn't thread-safe.
>
> --
> Thomas E. Dickeyhttp://invisible-island.netftp://invisible-...