# Second thread working at slow speed

**URL:** <https://www.ruby-forum.com/t/second-thread-working-at-slow-speed/191241>\
**Category:** wxRuby\
**Created:** [July 8, 2010, 12:42am UTC](https://www.ruby-forum.com/t/second-thread-working-at-slow-speed/191241 "2010-07-08T00:42:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![remosewa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@remosewa](https://www.ruby-forum.com/u/remosewa)\
**Post date:** [July 8, 2010, 12:42am UTC](https://www.ruby-forum.com/t/second-thread-working-at-slow-speed/191241/1 "2010-07-08T00:42:31Z")

</div>

I have program that does a bunch of file processing and the GUI froze up  
every time I ran it. I decided to do the trick of running my main code  
in a second thread and using the timer to update the gui every 100 ms.  
However, the problem is that the second thread that I run is working but  
very very slowly. Much slower than how it normally works.

I have the timer:  
timer = Timer.new(self,ID\_ANY)  
evt\_timer(timer.id) {Thread.pass}  
timer.start(1000)

my second thread:  
newthread = Thread.new do  
create\_prompt\_data  
create\_groups  
create\_xls  
end

---

<div class="post-metadata">

**Author:** ![remosewa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@remosewa](https://www.ruby-forum.com/u/remosewa)\
**Post date:** [July 8, 2010, 1:28am UTC](https://www.ruby-forum.com/t/second-thread-working-at-slow-speed/191241/2 "2010-07-08T01:28:40Z")

</div>

That’s cause whenever you pass the thread to another Ruby thread, it  
takes  
time away from the main thread. And since your Processing code is in a  
Secondary Thread, it gets less time then the Main Thread.

---

<div class="post-metadata">

**Author:** ![remosewa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@remosewa](https://www.ruby-forum.com/u/remosewa)\
**Post date:** [July 8, 2010, 8:07am UTC](https://www.ruby-forum.com/t/second-thread-working-at-slow-speed/191241/3 "2010-07-08T08:07:18Z")

</div>

Mario S. wrote:

> That’s cause whenever you pass the thread to another Ruby thread, it  
> takes  
> time away from the main thread. And since your Processing code is in a  
> Secondary Thread, it gets less time then the Main Thread.

No no, before I had created the second thread my computer would process  
the data and have a ton of CPU usage. Now with the thread it runs, but  
very very slowly and hardly any CPU usage.

---

<div class="post-metadata">

**Author:** ![remosewa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@remosewa](https://www.ruby-forum.com/u/remosewa)\
**Post date:** [July 8, 2010, 10:24am UTC](https://www.ruby-forum.com/t/second-thread-working-at-slow-speed/191241/4 "2010-07-08T10:24:12Z")

</div>

On 07/07/2010 23:42, Chase W. wrote:

> I have program that does a bunch of file processing and the GUI froze up  
> every time I ran it. I decided to do the trick of running my main code  
> in a second thread and using the timer to update the gui every 100 ms.

Is there any way you can break up the file processing into small  
discrete tasks? If so, you could could have a queue of jobs and consume  
them one at a time in an evt\_idle loop. This is a more efficient (and  
simpler) way of keeping the GUI responsive whilst handling long-running  
tasks.

Ruby 1.8 green threads + Wx::Timer has some overhead (thread context  
switching) and isn’t that efficient - it forces switching to the GUI  
thread when updates aren’t needed.

> However, the problem is that the second thread that I run is working but  
> very very slowly. Much slower than how it normally works.
> 
> I have the timer:  
> timer = Timer.new(self,ID\_ANY)  
> evt\_timer(timer.id) {Thread.pass}  
> timer.start(1000)

This is switching context every 1000ms - i.e. every second. Did you mean

timer.start(10)

or (simpler, if you don’t need later control)

Timer.every(10) { Thread.pass }

alex
