From: Masato Toyoshima Date: 2000-04-25T13:22:32+09:00 Subject: [ruby-list:22105] Re: Thread & STDIN.getc 豊島%自己フォローです STDIN.getcの部分をスレッドにすると、一文字だけの入力でも無事カウンターが 変化することが確認できました。 ただし、一文字でも入力すると、エンターを押すまで、Printが抑止されてしまい ます。これもCygwin版特有の動作でしょうか、、なんとか回避したいのですが、、 何か上手な方法がありましたら、ご教示くださいませ。 ==テストコード require "thread" def timeout(sec) begin x = Thread.current y = Thread.start { sleep sec x.raise TimeoutError, "execution expired" if x.status } yield sec return true ensure Thread.kill y if y.status end end b=1 za=Thread.start{ while true print "thread ",b," " b=b+1 end } nwrite=nil zb=Thread.start{ while true nstdin=nil timeout(0.01){ nstdin=STDIN.getc } case nstdin when nil pass when ?\n case nwrite when nil print "\n" else print "\nEnter入力後",b,"\n" print "入力文字数",nwrite.length,"\n" nwrite<<"\n" print nwrite nwrite=nil za.exit zb.exit end else case nwrite when nil print "一文字目の入力時",b,"\n" nwrite = nstdin.chr else nwrite << nstdin.chr end end end } while true exit if zb.status==nil exit if zb.status==false end ==ここまで =========================================== ^^ ^^ mailto:mt@izu.co.jp ^^ ^^ ^^ ^^ ^^ 豊島 正登 Masato Toyoshima ^^ ^^