# Ruby thread and gets

**URL:** <https://www.ruby-forum.com/t/ruby-thread-and-gets/174172>\
**Category:** Ruby\
**Created:** [August 21, 2009, 7:11pm UTC](https://www.ruby-forum.com/t/ruby-thread-and-gets/174172 "2009-08-21T19:11:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Im\_still](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/6de8d8/32.png) [@Im\_still](https://www.ruby-forum.com/u/Im_still)\
**Post date:** [August 21, 2009, 7:11pm UTC](https://www.ruby-forum.com/t/ruby-thread-and-gets/174172/1 "2009-08-21T19:11:40Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Im\_still](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/6de8d8/32.png) [@Im\_still](https://www.ruby-forum.com/u/Im_still)\
**Post date:** [August 21, 2009, 7:32pm UTC](https://www.ruby-forum.com/t/ruby-thread-and-gets/174172/2 "2009-08-21T19:32:09Z")

</div>

On 21.08.2009 19:10, Im still wrote:

> ```
> thread_a = Thread.new{
> 
> ```
> 
> …"  
> 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

---

<div class="post-metadata">

**Author:** ![Im\_still](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/6de8d8/32.png) [@Im\_still](https://www.ruby-forum.com/u/Im_still)\
**Post date:** [August 21, 2009, 9:13pm UTC](https://www.ruby-forum.com/t/ruby-thread-and-gets/174172/3 "2009-08-21T21:13:26Z")

</div>

2009/8/22 Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> > ```
> > thread_b = Thread.new{
> > 
> > ```
> > 
> > How can I handle this ?
> 
> Kind regards
> 
> ```
> robert
> 
> ```
> 
> –  
> remember.guy do |as, often| as.you\_can - without end  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)

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

---

<div class="post-metadata">

**Author:** ![Im\_still](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/6de8d8/32.png) [@Im\_still](https://www.ruby-forum.com/u/Im_still)\
**Post date:** [August 21, 2009, 11:08pm UTC](https://www.ruby-forum.com/t/ruby-thread-and-gets/174172/4 "2009-08-21T23:08:44Z")

</div>

2009/8/22 Im still [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> > > another one will do something else at the same time.  
> > > }  
> > > but when thread\_a runs gets waiting for user input, whole program will  
> > > Do I have to do this by running multi process?  
> > > [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)
> 
> 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.

---

<div class="post-metadata">

**Author:** ![Im\_still](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/6de8d8/32.png) [@Im\_still](https://www.ruby-forum.com/u/Im_still)\
**Post date:** [August 22, 2009, 11:05am UTC](https://www.ruby-forum.com/t/ruby-thread-and-gets/174172/5 "2009-08-22T11:05:45Z")

</div>

On 21.08.2009 21:41, Im still wrote:

> 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

---

<div class="post-metadata">

**Author:** ![Im\_still](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/6de8d8/32.png) [@Im\_still](https://www.ruby-forum.com/u/Im_still)\
**Post date:** [August 22, 2009, 6:27pm UTC](https://www.ruby-forum.com/t/ruby-thread-and-gets/174172/6 "2009-08-22T18:27:05Z")

</div>

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…

2009/8/22 Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
