"stack level too deep"... because Threads keep their "starting" stack

S

Sam Roberts

It appears threads keep their callers stack. Reasonable. But they do
this when their caller was a thread, and it can lead to problems.

Is there any way to strip your calling context, or does it break the
language somehow? Is it needed for purposes other than debugging?

Cheers,
Sam


$ ruby18 xthrd.rb
................................................................................................................................................................................................................................stack level too deep

$ cat xthrd.rb

$stdout.sync = true

class Cache
def resweep( period )
Thread.new do
begin
#sleep( period )
sweep_cache
rescue
puts $!
# $!.backtrace.each { |s| puts s }
exit 1
end
# then return, so we cease to exist, freeing our resources... right?
end
end

def sweep_cache
$stdout.write "."
resweep(1)
end

end


c = Cache.new

c.resweep(1)

loop do
sleep 60
end


^^^^^^^^^^^^


What I was trying to do was when I saw something needed to be done to
the cache in some amount of seconds, spin off a Thread that would sleep
for those seconds, then do the work.

However, the work might involve noticing new work to be done some time.
So, spin off another thread to do the work, and finish your own work. No
problem... ???

I'm going to do this differently, but I thought it was interesting and
unexpected.
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: "stack level too deep"... because Threads keep their "starting" stack"

|Is there any way to strip your calling context, or does it break the
|language somehow? Is it needed for purposes other than debugging?

No way to strip. It's a limitation of the current thread
implementation.

matz.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,765
Messages
2,569,568
Members
45,042
Latest member
icassiem

Latest Threads

Top