# Ruby and threads: a VTK example

**URL:** <https://www.ruby-forum.com/t/ruby-and-threads-a-vtk-example/75404>\
**Category:** Ruby\
**Created:** [November 9, 2006, 6:09pm UTC](https://www.ruby-forum.com/t/ruby-and-threads-a-vtk-example/75404 "2006-11-09T18:09:18Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![buendia](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/54ee81/32.png) [@buendia](https://www.ruby-forum.com/u/buendia)\
**Post date:** [November 9, 2006, 6:09pm UTC](https://www.ruby-forum.com/t/ruby-and-threads-a-vtk-example/75404/1 "2006-11-09T18:09:18Z")

</div>

Hi,

There is a ruby wrapper available for vtk which works, more or less,  
fine. For those who are not familiar with vtk, it is avisualisation  
library written in c++ and the output is a native window showing some  
interactive OpenGL graphics.

The VTK code usually ends with:

iren = Vtk::RenderWindowInteractor.new  
iren.start()

Here is where the problem comes: iren.start() starts a new thread in  
c++, while, I am trying to run the script in irb and interactively  
change the objects properties and see the live results in the OpenGL  
window. Unfortunately, after iren.start() ruby goes to a deep sleep.  
This is the irb output:

> >

?\> iren.Start()

which obviously does not let me to enter any more commands unless I  
close the window:

=\> nil

> >

which is too late for manipulating objects interactively.

It is surprising when a multi-threaded c++ library is single-threaded  
after wrapping to ruby. If this does something have to do with ruby not  
supporting native threads, perhaps it is a big sacrifice for  
portability.

What is the best way to have irb command line, while also having the  
graphics rendering window?

---

<div class="post-metadata">

**Author:** ![buendia](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/54ee81/32.png) [@buendia](https://www.ruby-forum.com/u/buendia)\
**Post date:** [November 9, 2006, 6:25pm UTC](https://www.ruby-forum.com/t/ruby-and-threads-a-vtk-example/75404/2 "2006-11-09T18:25:34Z")

</div>

On Fri, 2006-11-10 at 02:09 +0900, Aureliano B. wrote:

> Here is where the problem comes: iren.start() starts a new thread in
> 
> =\> nil
> 
> > >
> 
> which is too late for manipulating objects interactively.

I don’t have any of this stuff installed, so I’m guessing, but couldn’t  
you just do e.g.:

winthr = Thread.new { iren.Start() }

// do whatever else you need to do

winthr.kill # or close the window may be better

Obviously I don’t know about the concurrency issues but with this being  
interactive I’d guess it’s not something you’d worry much about.

---

<div class="post-metadata">

**Author:** ![buendia](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/54ee81/32.png) [@buendia](https://www.ruby-forum.com/u/buendia)\
**Post date:** [November 9, 2006, 7:09pm UTC](https://www.ruby-forum.com/t/ruby-and-threads-a-vtk-example/75404/3 "2006-11-09T19:09:52Z")

</div>

On Fri, 2006-11-10 at 02:36 +0900, Aureliano B. wrote:

> ?\> winthr = Thread.new { iren.Start() }
> 
> Still no command line. All I want is a command line with access to the  
> the objects in the running thread.

Well, it looks like something in the library is blocking the calling  
process (and all Ruby’s green threads) completely. If so, it’s probably  
something you’d have to talk to the binding’s maintainer about.

I did just try to install VTK but it barfed horribly on my Linux box so  
I’m afraid I can’t be much more help.

> You do not need to have access to this library, it can be emulated  
> easily: Assume you want to run a very long process names  
> VeryLongProcess(). I want a command line when VeryLongProcess() is  
> running and I want this command line to have acees to all the objects  
> that VeryLongProcess() has access to them.

Assuming you mean ‘process’ in the sense of ‘something happening in  
Ruby’, then the thread thing I suggested should work. If OTOH I’m  
missing something and you’re referring to actual processes, then it gets  
more involved…

---

<div class="post-metadata">

**Author:** ![buendia](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/54ee81/32.png) [@buendia](https://www.ruby-forum.com/u/buendia)\
**Post date:** [November 9, 2006, 6:36pm UTC](https://www.ruby-forum.com/t/ruby-and-threads-a-vtk-example/75404/4 "2006-11-09T18:36:03Z")

</div>

Ross B. wrote:

> winthr = Thread.new { iren.Start() }
> 
> // do whatever else you need to do
> 
> winthr.kill # or close the window may be better

I get the same thing:

?\> winthr = Thread.new { iren.Start() }

Still no command line. All I want is a command line with access to the  
the objects in the running thread.

You do not need to have access to this library, it can be emulated  
easily: Assume you want to run a very long process names  
VeryLongProcess(). I want a command line when VeryLongProcess() is  
running and I want this command line to have acees to all the objects  
that VeryLongProcess() has access to them.

---

<div class="post-metadata">

**Author:** ![buendia](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/54ee81/32.png) [@buendia](https://www.ruby-forum.com/u/buendia)\
**Post date:** [November 9, 2006, 7:21pm UTC](https://www.ruby-forum.com/t/ruby-and-threads-a-vtk-example/75404/5 "2006-11-09T19:21:29Z")

</div>

Ross,

Forget about vtk, put it this way. Save this in a script and run it by  
irb:

i = 1  
while 1  
puts i  
end

It doesn’t allow you to add any more command lines unless the  
never-ending while-loop is over. Wrapping this in a thread does not help  
it too:

Thread.new{  
i = 1  
while 1  
puts i  
end  
}

All I want to do is to have the while loop, and then enter

i = 2

in the command line to get 2 printed.

---

<div class="post-metadata">

**Author:** ![buendia](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/54ee81/32.png) [@buendia](https://www.ruby-forum.com/u/buendia)\
**Post date:** [November 9, 2006, 7:51pm UTC](https://www.ruby-forum.com/t/ruby-and-threads-a-vtk-example/75404/6 "2006-11-09T19:51:59Z")

</div>

On Thu, 09 Nov 2006 18:21:38 -0000, Aureliano B.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> It doesn’t allow you to add any more command lines unless the  
> never-ending while-loop is over.

True.

> Wrapping this in a thread does not help it too:
> 
> Thread.new{  
> i = 1  
> while 1  
> puts i  
> end  
> }

Apart from the fact that the output makes it difficult to type, that  
works  
fine for me, with one small change - you need to declare ‘i’ before you  
start the thread - otherwise, it’ll be local to the block you run in the  
thread. So try (rubified a bit):

i = 1

Thread.new { loop { puts i } }

You’ll get lots of '1’s printed out. If you just jump in and type ‘i =  
2’  
and hit return, the '1’s become '2’s. Does that not work for you?

---

<div class="post-metadata">

**Author:** ![buendia](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/54ee81/32.png) [@buendia](https://www.ruby-forum.com/u/buendia)\
**Post date:** [November 10, 2006, 3:06pm UTC](https://www.ruby-forum.com/t/ruby-and-threads-a-vtk-example/75404/7 "2006-11-10T15:06:21Z")

</div>

From: Aureliano B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Subject: Re: Ruby and threads: a VTK example  
Date: Fri, 10 Nov 2006 03:21:38 +0900  
Message-ID: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> It doesn’t allow you to add any more command lines unless the  
> never-ending while-loop is over. Wrapping this in a thread does not help  
> it too:

What is your OS ?  
If it is Windows, your trouble may depends on console I/O.  
Console I/O of Win (e.g. to get a conmmand line on IRB)  
blocks thread-switching of Ruby.

# Please see [ruby-talk:223935] also.

---

<div class="post-metadata">

**Author:** ![buendia](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/54ee81/32.png) [@buendia](https://www.ruby-forum.com/u/buendia)\
**Post date:** [November 10, 2006, 6:18pm UTC](https://www.ruby-forum.com/t/ruby-and-threads-a-vtk-example/75404/8 "2006-11-10T18:18:14Z")

</div>

Hidetoshi NAGAI wrote:

> > It doesn’t allow you to add any more command lines unless the  
> > never-ending while-loop is over. Wrapping this in a thread does not help  
> > it too:
> 
> What is your OS ?  
> If it is Windows, your trouble may depends on console I/O.  
> Console I/O of Win (e.g. to get a conmmand line on IRB)  
> blocks thread-switching of Ruby.
> 
> # Please see [ruby-talk:223935] also.

Hidetoshi,

The OS is WinXP, I have also tried IRB with eclipse as an external tool  
with no luck. What Ross described does not work on windows as he has  
ruby on a linux box. The example you mentioned in runy-talk was done  
with tk. Is there any ways or doing this in simple dos command prompt  
(or eclipse)? I expected ruby to me more portable than this!

---

<div class="post-metadata">

**Author:** ![buendia](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/54ee81/32.png) [@buendia](https://www.ruby-forum.com/u/buendia)\
**Post date:** [November 11, 2006, 3:11pm UTC](https://www.ruby-forum.com/t/ruby-and-threads-a-vtk-example/75404/9 "2006-11-11T15:11:26Z")

</div>

From: Aureliano B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Subject: Re: Ruby and threads: a VTK example  
Date: Sat, 11 Nov 2006 02:18:18 +0900  
Message-ID: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> The example you mentioned in runy-talk was done  
> with tk. Is there any ways or doing this in simple dos command prompt  
> (or eclipse)?

I’m sorry, but I have no idea. If I had, I didn’t make irbtkw.rbw.😉  
Probably, it requires rewriting IRB::StdioInputMethod#gets.  
I don’t know how it has to be changed, because I’m not familiar with  
programing on Windows.
