ruby thread and gets

I

Im still

[Note: parts of this message were removed to make it a legal post.]

Hi all
I'm trying to run two thread at one time in a program.
And one of them will receive user inputting msgs by gets().
another one will do something else at the same time.
they don't communicate each other.

like this :

thread_a = Thread.new{
loop do
puts "please enter blah blah"
msg = gets
#do something
end
}
thread_a.join
thread_b = Thread.new{
loop do
#do other things maybe puts "im running im running
......"
end

}
thread_b.join

but when thread_a runs gets waiting for user input, whole program will be
paused waiting for this, I mean thread_b will not working.
How can I handle this ?
Do I have to do this by running multi process?
 
R

Robert Klemme

[Note: parts of this message were removed to make it a legal post.]

Hi all
I'm trying to run two thread at one time in a program.
And one of them will receive user inputting msgs by gets().
another one will do something else at the same time.
they don't communicate each other.

like this :

thread_a = Thread.new{
loop do
puts "please enter blah blah"
msg = gets
#do something
end
}
thread_a.join
thread_b = Thread.new{
loop do
#do other things maybe puts "im running im running
....."
end

}
thread_b.join

but when thread_a runs gets waiting for user input, whole program will be
paused waiting for this, I mean thread_b will not working.
How can I handle this ?

You are using threads like ordinary functions: you start a thread and
wait for it to finish before you start the next one. You should move
thread_a.join after the start of thread_b.
Do I have to do this by running multi process?

No.

Kind regards

robert
 
I

Im still

[Note: parts of this message were removed to make it a legal post.]

2009/8/22 Robert Klemme said:
[Note: parts of this message were removed to make it a legal post.]


Hi all
I'm trying to run two thread at one time in a program.
And one of them will receive user inputting msgs by gets().
another one will do something else at the same time.
they don't communicate each other.

like this :

thread_a = Thread.new{
loop do
puts "please enter blah blah"
msg = gets
#do something
end
}
thread_a.join
thread_b = Thread.new{
loop do
#do other things maybe puts "im running im running
....."
end

}
thread_b.join

but when thread_a runs gets waiting for user input, whole program will be
paused waiting for this, I mean thread_b will not working.
How can I handle this ?

You are using threads like ordinary functions: you start a thread and wait
for it to finish before you start the next one. You should move
thread_a.join after the start of thread_b.

Do I have to do this by running multi process?
No.

Kind regards

robert




Thanks Robert. Seems I didn't uderstand well with thr.join method...
 
I

Im still

[Note: parts of this message were removed to make it a legal post.]

2009/8/22 Im still said:
2009/8/22 Robert Klemme said:
[Note: parts of this message were removed to make it a legal post.]


Hi all
I'm trying to run two thread at one time in a program.
And one of them will receive user inputting msgs by gets().
another one will do something else at the same time.
they don't communicate each other.

like this :

thread_a = Thread.new{
loop do
puts "please enter blah blah"
msg = gets
#do something
end
}
thread_a.join
thread_b = Thread.new{
loop do
#do other things maybe puts "im running im running
....."
end

}
thread_b.join

but when thread_a runs gets waiting for user input, whole program will be
paused waiting for this, I mean thread_b will not working.
How can I handle this ?

You are using threads like ordinary functions: you start a thread and wait
for it to finish before you start the next one. You should move
thread_a.join after the start of thread_b.

Do I have to do this by running multi process?
No.

Kind regards

robert




Thanks Robert. Seems I didn't uderstand well with thr.join method...



Still a question.
How can I implement a function that command line will keep on outputting
messages while it will still receive user's keyboard inputtings just like
Linux shell always does?

thanks.
 
R

Robert Klemme

Still a question.
How can I implement a function that command line will keep on outputting
messages while it will still receive user's keyboard inputtings just like
Linux shell always does?

Not sure what you mean: the console will happily print whenever
something is sent to it for printing. It seems your question is rather
about how to have work done in background while waiting for user input
in the foreground. I am not sure though whether it is a good idea to
clutter the terminal with output from background jobs when you want to
receive typed user input at the same time. It can be really hard to
keep track of what you typed when background output is copious.

Kind regards

robert
 
I

Im still

[Note: parts of this message were removed to make it a legal post.]

Actually, I'm trying to write a command line chatting program. Just for fun.
With UDP.
And having troubles on controlling (managing ) threads between outputting
received msg from other client and getting user typed commands.
Because, gets() method seems paused whole program, and if there are messages
received from other client will not be displayed automatically.program will
stay waiting for user input.....
I don't know if I've told things clear enough....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,740
Latest member
AdolphBig6

Latest Threads

Top