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.
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.