From: Matt Armstrong Date: 2002-04-21T04:08:05+09:00 Subject: Re: Threads creating threads creating threads... Tobias Peters writes: > My previous posting was not clear enough, I'll try to enhance it. > > I include a ruby program in this message that should behave like > this: > > 1) The main thread creates child thread with No n=1 > 2) Child thread n creates child thread n+1 > 3) Child thread n exits and some other thread calls join upon it > 4) Goto 2) with n=n+1 > > So this is an endless loop, the example program should run forever, > and threads are created and destroyed as it runs. The memory usage > of this program should be constant, because at any given time, there > are at most 3 threads living simultaneously. Ideally, your program would run forever. But when a new thread is created, the stack of the current thread is included in the new thread. So if a child thread always spawns another thread the stack size will grow infinitely. Ruby's thread implementation isn't the best. It is slow since switching threads involves physically copying the stack memory. And you discovered another problem -- a new thread will always use the stack memory of the thread that created it even though it will never be used. Despite the weaknesses, Ruby's threads are very portable since they rely only on setjmp()/longjmp(). Maybe there are other reasons for the design too? -- matt