From: "Jesús Gabriel y Galán" Date: 2010-05-26T19:19:09+09:00 Subject: Re: how to achieve parallelism, using threads? On Wed, May 26, 2010 at 12:07 PM, Walle Wallen wrote: > 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. 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.