# How to achieve parallelism, using threads?

**URL:** <https://www.ruby-forum.com/t/how-to-achieve-parallelism-using-threads/189189>\
**Category:** Ruby\
**Created:** [May 26, 2010, 12:07pm UTC](https://www.ruby-forum.com/t/how-to-achieve-parallelism-using-threads/189189 "2010-05-26T12:07:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![walle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/5daacb/32.png) [@walle](https://www.ruby-forum.com/u/walle)\
**Post date:** [May 26, 2010, 12:07pm UTC](https://www.ruby-forum.com/t/how-to-achieve-parallelism-using-threads/189189/1 "2010-05-26T12:07:21Z")

</div>

Hey,  
I’m having a hard time figuring out how threads works, and how to use  
them. I’m currently working on a project were I need to achieve  
parallelism in the code, but I always ends up with a multiply threads,  
working in a queue (after each other, as a single thread program).

If we take a look at the code example bellow. What I’m trying to achieve  
is the following.  
The main loop starts a new thread, executes it and then CONTINUES,  
without waiting for  
the thread to finish. They should work simultaneously.

The problem is, they don’t. So, I tried executing the thread without  
using join. But it crashed  
the program since the thread didn’t have time to finish.

Can someone please help me?

//Walle

Example code  
def func()  
#Tcp connection and more  
end

def main\_loop()  
#do some work  
aThread = Thread.new {func()}  
aThread.join  
end

main\_loop()

---

<div class="post-metadata">

**Author:** ![walle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/5daacb/32.png) [@walle](https://www.ruby-forum.com/u/walle)\
**Post date:** [May 26, 2010, 12:19pm UTC](https://www.ruby-forum.com/t/how-to-achieve-parallelism-using-threads/189189/2 "2010-05-26T12:19:27Z")

</div>

On Wed, May 26, 2010 at 12:07 PM, Walle W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> the thread to finish. They should work simultaneously.  
> When you call join, the main thread waits for the other to complete.

> The problem is, they don’t. So, I tried executing the thread without  
> using join. But it crashed  
> the program since the thread didn’t have time to finish.

So you want the main thread to do some work in parallel with the other  
thread but then wait for it before exiting the program?  
Then, you should start the thread, then do main’s work, and then  
finally join the thread:

def func()  
#Tcp connection and more  
end

def main\_loop()  
aThread = Thread.new {func()}  
#do some work  
aThread.join  
end

main\_loop()

Jesus.

---

<div class="post-metadata">

**Author:** ![walle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/5daacb/32.png) [@walle](https://www.ruby-forum.com/u/walle)\
**Post date:** [May 26, 2010, 12:38pm UTC](https://www.ruby-forum.com/t/how-to-achieve-parallelism-using-threads/189189/3 "2010-05-26T12:38:17Z")

</div>

> So you want the main thread to do some work in parallel with the other  
> thread but then wait for it before exiting the program?  
> Then, you should start the thread, then do main’s work, and then  
> finally join the thread:

That achieves the same result, or close to it. I don’t want the main  
thread to wait for the second thread do finish, but in the same  
time I have to, otherwise the program crashes.

So, the main threads should launch a seconds thread, and then  
continue with it’s work, and ignore the second thread. The  
seconds thread should run in it’s on speed, and don’t have to  
finish before the main thread/loop does.

Hehe, I’m having a hard time explaining what I’m trying to achieve

//Walle

---

<div class="post-metadata">

**Author:** ![walle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/5daacb/32.png) [@walle](https://www.ruby-forum.com/u/walle)\
**Post date:** [May 26, 2010, 12:41pm UTC](https://www.ruby-forum.com/t/how-to-achieve-parallelism-using-threads/189189/4 "2010-05-26T12:41:07Z")

</div>

On Wed, May 26, 2010 at 12:38 PM, Walle W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> So, the main threads should launch a seconds thread, and then  
> continue with it’s work, and ignore the second thread. The  
> seconds thread should run in it’s on speed, and don’t have to  
> finish before the main thread/loop does.
> 
> Hehe, I’m having a hard time explaining what I’m trying to achieve

I think it’s clear, and what you have to do is what I explained. The  
main thread launches the second thread and then goes on with its work.  
Once the main thread finished all its work and it’s about to exit the  
program, that’s when you join the other thread, to ensure it finishes.

Jesus.

---

<div class="post-metadata">

**Author:** ![walle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/5daacb/32.png) [@walle](https://www.ruby-forum.com/u/walle)\
**Post date:** [May 26, 2010, 12:56pm UTC](https://www.ruby-forum.com/t/how-to-achieve-parallelism-using-threads/189189/5 "2010-05-26T12:56:51Z")

</div>

> I think it’s clear, and what you have to do is what I explained. The  
> main thread launches the second thread and then goes on with its work.  
> Once the main thread finished all its work and it’s about to exit the  
> program, that’s when you join the other thread, to ensure it finishes.
> 
> Jesus.

Okey, I think I got it.  
Thanks, I really appreciate

---

<div class="post-metadata">

**Author:** ![walle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/5daacb/32.png) [@walle](https://www.ruby-forum.com/u/walle)\
**Post date:** [May 26, 2010, 12:57pm UTC](https://www.ruby-forum.com/t/how-to-achieve-parallelism-using-threads/189189/6 "2010-05-26T12:57:15Z")

</div>

Walle W. wrote:

> > I think it’s clear, and what you have to do is what I explained. The  
> > main thread launches the second thread and then goes on with its work.  
> > Once the main thread finished all its work and it’s about to exit the  
> > program, that’s when you join the other thread, to ensure it finishes.
> > 
> > Jesus.
> 
> Okey, I think I got it.  
> Thanks, I really appreciate

it.  
//Walle
