From: Robert Klemme Date: 2010-12-27T19:34:35+09:00 Subject: Re: background threads On Mon, Dec 20, 2010 at 11:24 PM, Venkat Akkineni wrote: > Hello Robert > > > Thanks for your code and help. By commenting a few lines in your code I > will try to make my argument. > > > if you comment out the following lines in the code above. You will > notice > that the thread exists only momentarily even with the join. It won't > even print the "Background" the first time. > > #10.times do |i| >  #  printf "%-20s %5d\n", "Foreground", i >  # sleep 3 > #end Well, obviously because - as you state below - when the main thread dies the interpreter exits and takes all other threads with it. > While I could use a loop like the one above, it will stop all execution > at that point until the hamster has run its length. How can I change the > code so that the main thread respects the currently running thread and > let it do its thing before it exists. You need to join as I showed in my example. > Notice that I have read the > documentation and understand that when main thread exits it will kill > all the running threads. > > Also my code attached to the email. It is a simple window class > that uses SDL. SDL window stays alive as long as there is a loop right > next to window initialization. > > -> When add the loop in a method and call it from some where else . The > execution stops at the line I am calling it. > -> So I thought creating a new thread will help, but when I create a > thread window appears for an instant and disappears. > -> I try adding thread.join right after creating an instance of a class > that encloses window creation. This causes freezing of the execution at > thread.join I don't know SDL so I cannot comment on that. Maybe you want to do the window handling in the main thread. > I am pretty sure that I am going about this the wrong way but not sure > what is the right way. Your main thread needs to do what it needs to do. If it's done with that it can join the background thread. As simple as that. But first you should probably get a clear understanding which things need to be done and which need to be done concurrently. It may turn out that you don't need any additional threads. Also, since apparently you want to use a windowing system you should check its documentation because these tend to have some requirements of their own. Cheers robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/