A
Abraham Tio
need a class(we'll call it a 'simulator' since that's what i'm
attempting to write) to run a loop.
need that loop to stop when asked to, such as when the user that started
it clicks a button to stop it.
i write java that does this sort of thing all the time.
HOWEVER, when i try it in ruby, only bad things happen.
check this out:
***start r.rb***
class Simulator
attr_accessor :running
attr_accessor :sim_thread
def start
running = true;
puts "starting simulator..."
sim_thread= Thread.new(){
puts "sim thread started"
while(running)
300000.times do |x|
x+=1
end
puts 'did a buncha stuff a simulator might do'
Thread.pass
end
puts "sim thread stopped"
}
sim_thread.join
sim_thread
end
def stop
puts "stopping simulator..."
running = false
end
end
Thread.abort_on_exception = true
simulator = Simulator.new
thread = simulator.start
puts "started simulator, now we stop it.."
simulator.stop
***end r.rb***
i get no exceptions in the thread.
the output:
starting simulator...
sim thread started
did a buncha stuff a simulator might do
did a buncha stuff a simulator might do
did a buncha stuff a simulator might do
this line never executes:
puts "started simulator, now we stop it.."
why does ruby not leave the thread lone to do its thing,
and do other things ?
instead, it seems to get stuck in the while loop, as if i didn't put it
in a separate thread.
any help much appreciated, thanks.
attempting to write) to run a loop.
need that loop to stop when asked to, such as when the user that started
it clicks a button to stop it.
i write java that does this sort of thing all the time.
HOWEVER, when i try it in ruby, only bad things happen.
check this out:
***start r.rb***
class Simulator
attr_accessor :running
attr_accessor :sim_thread
def start
running = true;
puts "starting simulator..."
sim_thread= Thread.new(){
puts "sim thread started"
while(running)
300000.times do |x|
x+=1
end
puts 'did a buncha stuff a simulator might do'
Thread.pass
end
puts "sim thread stopped"
}
sim_thread.join
sim_thread
end
def stop
puts "stopping simulator..."
running = false
end
end
Thread.abort_on_exception = true
simulator = Simulator.new
thread = simulator.start
puts "started simulator, now we stop it.."
simulator.stop
***end r.rb***
i get no exceptions in the thread.
the output:
starting simulator...
sim thread started
did a buncha stuff a simulator might do
did a buncha stuff a simulator might do
did a buncha stuff a simulator might do
this line never executes:
puts "started simulator, now we stop it.."
why does ruby not leave the thread lone to do its thing,
and do other things ?
instead, it seems to get stuck in the while loop, as if i didn't put it
in a separate thread.
any help much appreciated, thanks.