# Thread problem

**URL:** <https://www.ruby-forum.com/t/thread-problem/116731>\
**Category:** Ruby\
**Created:** [October 9, 2007, 5:04am UTC](https://www.ruby-forum.com/t/thread-problem/116731 "2007-10-09T05:04:03Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mokkai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c2a13f/32.png) [@mokkai](https://www.ruby-forum.com/u/mokkai)\
**Post date:** [October 9, 2007, 5:04am UTC](https://www.ruby-forum.com/t/thread-problem/116731/1 "2007-10-09T05:04:03Z")

</div>

i want to make a program code ,but it ll take long time to execute  
so i am spiliting my program into thread …

but the general concept about thread is ,it will reduce the execution  
time of our program under good condition (depending RAM size,Processor  
speed ,etc…)

But if the number of thread is more than that capacity of RAM ,Processor  
speed,and other resouces…then the program will be very very slow

so i want to check all the resource status(RAM ,processor,etc…) ,after  
creating everyone child thread from parent thread …

so how can i find the resource status(RAM ,processor,etc…) in the  
parent thread .  
so i can decide about to create next child thread…

any idea…

---

<div class="post-metadata">

**Author:** ![mokkai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c2a13f/32.png) [@mokkai](https://www.ruby-forum.com/u/mokkai)\
**Post date:** [October 9, 2007, 5:24am UTC](https://www.ruby-forum.com/t/thread-problem/116731/2 "2007-10-09T05:24:03Z")

</div>

1.) The only way threads get you a speedup is when you write your code  
in  
such a way that sections of it can be executed in parallel so you can  
take  
advantage of multiple cores (or processors).

2.) The current version of Ruby only supports green threads anyhow. So  
even  
if you did have multiple cores, you wouldn’t get a speedup.

MBL

---

<div class="post-metadata">

**Author:** ![mokkai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c2a13f/32.png) [@mokkai](https://www.ruby-forum.com/u/mokkai)\
**Post date:** [October 9, 2007, 6:08am UTC](https://www.ruby-forum.com/t/thread-problem/116731/3 "2007-10-09T06:08:04Z")

</div>

Michael Bevilacqua-Linn wrote:

> 2.) The current version of Ruby only supports green threads anyhow. So  
> even  
> if you did have multiple cores, you wouldn’t get a speedup.
> 
> MBL

green thread means ?..

---

<div class="post-metadata">

**Author:** ![mokkai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c2a13f/32.png) [@mokkai](https://www.ruby-forum.com/u/mokkai)\
**Post date:** [October 9, 2007, 10:35am UTC](https://www.ruby-forum.com/t/thread-problem/116731/4 "2007-10-09T10:35:37Z")

</div>

Michael Bevilacqua-Linn wrote:

> 1.) The only way threads get you a speedup is when you write your code  
> in  
> such a way that sections of it can be executed in parallel so you can  
> take  
> advantage of multiple cores (or processors).
> 
> 2.) The current version of Ruby only supports green threads anyhow. So  
> even  
> if you did have multiple cores, you wouldn’t get a speedup.
> 
> MBL

Then perhaps you could explain the output of this program:

require ‘rtiming’ #a simple program that times methods

def test1  
threads = []

2.times do |i|  
threads[i] = Thread.new do  
sleep(2)  
puts “task #{i+1} finished”  
end  
end

threads.each do |t|  
t.join  
end  
end

def test2  
sleep(2)  
puts “task 1 finished”

sleep(2)  
puts “task 2 finished”  
end

puts timer(:test1, 1)  
puts timer(:test2, 1)

–output:—  
task 2 finished  
task 1 finished  
Method exec time(1 loops): 2.010791 total  
task 1 finished  
task 2 finished  
Method exec time(1 loops): 4.000226 total

---

<div class="post-metadata">

**Author:** ![mokkai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c2a13f/32.png) [@mokkai](https://www.ruby-forum.com/u/mokkai)\
**Post date:** [October 9, 2007, 6:42am UTC](https://www.ruby-forum.com/t/thread-problem/116731/5 "2007-10-09T06:42:23Z")

</div>

Pokkai D. wrote:

> Michael Bevilacqua-Linn wrote:
> 
> > 2.) The current version of Ruby only supports green threads anyhow. So  
> > even  
> > if you did have multiple cores, you wouldn’t get a speedup.
> > 
> > MBL
> 
> green thread means ?..

threads managed by the language you’re working in rather than by the  
operating system

> **[Green thread](https://en.wikipedia.org/wiki/Green_threads)**
>
> In computer programming, a green thread (virtual thread) is a thread that is scheduled by a runtime library or virtual machine (VM) instead of natively by the underlying operating system (OS). Green threads emulate multithreaded environments without relying on any native OS abilities, and they are managed in user space instead of kernel space, enabling them to work in environments that do not have native thread support.
> Green threads refers to the name of the original thread library for the prog...

–gw

---

<div class="post-metadata">

**Author:** ![mokkai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c2a13f/32.png) [@mokkai](https://www.ruby-forum.com/u/mokkai)\
**Post date:** [October 9, 2007, 4:04pm UTC](https://www.ruby-forum.com/t/thread-problem/116731/6 "2007-10-09T16:04:23Z")

</div>

Um,

All you’re doing is creating threads and putting them to sleep…  
You’re  
not doing any actual work.

What are you getting at exactly?

MBL

---

<div class="post-metadata">

**Author:** ![mokkai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c2a13f/32.png) [@mokkai](https://www.ruby-forum.com/u/mokkai)\
**Post date:** [October 9, 2007, 5:51pm UTC](https://www.ruby-forum.com/t/thread-problem/116731/7 "2007-10-09T17:51:18Z")

</div>

On Tue, 9 Oct 2007, Michael Bevilacqua-Linn wrote:

> 1.) The only way threads get you a speedup is when you write your code in  
> such a way that sections of it can be executed in parallel so you can take  
> advantage of multiple cores (or processors).
> 
> 2.) The current version of Ruby only supports green threads anyhow. So even  
> if you did have multiple cores, you wouldn’t get a speedup.

It’s not that simple. With Ruby’s green threads, it all depends on  
whether there’s a latency somewhere, in one thread, that can allow  
another  
to make use of that time and get something done.

You aren’t going to find that latency in any CPU bound operation, and  
you  
aren’t going to find that latency in any extension to Ruby that was not  
written to be Ruby aware.

If your problem is IO bound, though, ruby can capture that time where  
one  
thread is waiting on IO to let another thtread do something. In those  
cases, you can get a speedup from Ruby’s green threads.

Kirk H.

---

<div class="post-metadata">

**Author:** ![mokkai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c2a13f/32.png) [@mokkai](https://www.ruby-forum.com/u/mokkai)\
**Post date:** [October 9, 2007, 6:12pm UTC](https://www.ruby-forum.com/t/thread-problem/116731/8 "2007-10-09T18:12:27Z")

</div>

Ah probably what 7stud7 was getting at as well, and yeah, you’re right.

However, if you read the OPs description, an i/o bound problem doesn’t  
seem  
likely.

MBL
