a loop in a thread... very greedy

Discussion in 'Ruby' started by Abraham Tio, Dec 31, 2007.

  1. Abraham Tio

    Abraham Tio Guest

    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.
    --
    Posted via http://www.ruby-forum.com/.
     
    Abraham Tio, Dec 31, 2007
    #1
    1. Advertising

  2. Abraham Tio

    Gary Wright Guest

    On Dec 30, 2007, at 10:52 PM, Abraham Tio wrote:
    >
    > sim_thread.join
    >


    You're explicitly asking the main thread to let
    the sim_thread run to completion. Don't join
    with another thread if you don't want to wait
    for it to exit.

    Gary Wright
     
    Gary Wright, Dec 31, 2007
    #2
    1. Advertising

  3. > why does ruby not leave the thread lone to do its thing,
    > and do other things ?

    it does ... usually, but since you're explicitly joining it, it'll wait
    in start method untill thread finishes

    >
    > instead, it seems to get stuck in the while loop, as if i didn't put it
    > in a separate thread.

    You did, and then you turned multi threaded application into single
    threaded one

    >
    > any help much appreciated, thanks.

    remove join. read documentation (as java user you should be used to it)
     
    Marcin Raczkowski, Dec 31, 2007
    #3
  4. Abraham Tio

    s.ross Guest

    On Dec 30, 2007, at 7:52 PM, Abraham Tio wrote:

    > Thread.abort_on_exception = true
    > simulator = Simulator.new
    > thread = simulator.start
    > puts "started simulator, now we stop it.."
    > simulator.stop


    Others have pointed out not to use join. Of course, once you remove
    join, the start and stop methods are invoked one right after another,
    so you must put a sleep(n) between the two to observe the child thread
    running independent of the main thread.
     
    s.ross, Dec 31, 2007
    #4
  5. On Dec 30, 2007, at 7:52 PM, Abraham Tio wrote:

    > 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


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


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



    You have an attr_accessor :running but you were assigning to a
    running local variable. You need to either use @running = true or
    self.running = true> When ruby sees 'running = true' it decides that
    running is a local variable, so using running = does not set the
    @running ivar via the attr_acessor but self.running = true does.

    Cheers-
    - Ezra Zygmuntowicz
    -- Founder & Software Architect
    --
    -- EngineYard.com
     
    Ezra Zygmuntowicz, Dec 31, 2007
    #5
  6. Abraham Tio wrote:
    > 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:
    >


    here a woking code

    class Simulator
    attr_accessor :running
    attr_accessor :sim_thread
    def start
    @running = true; #instance var
    puts "starting simulator..."
    @sim_thread= Thread.new(){ #instance var
    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 no 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
    # wait simulator end
    simulator.sim_thread.join

    Enjoy!
    --
    Posted via http://www.ruby-forum.com/.
     
    Tiziano Merzi, Dec 31, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Raymond Arthur St. Marie II of III

    very Very VERY dumb Question About The new Set( ) 's

    Raymond Arthur St. Marie II of III, Jul 23, 2003, in forum: Python
    Replies:
    4
    Views:
    518
    Raymond Hettinger
    Jul 27, 2003
  2. shanx__=|;-

    very very very long integer

    shanx__=|;-, Oct 16, 2004, in forum: C Programming
    Replies:
    19
    Views:
    1,718
    Merrill & Michele
    Oct 19, 2004
  3. Dan Kelly

    Greedy and non greedy quantifiers

    Dan Kelly, Jan 17, 2008, in forum: Ruby
    Replies:
    4
    Views:
    168
    Robert Klemme
    Jan 19, 2008
  4. Matt Garrish

    greedy v. non-greedy matching

    Matt Garrish, Feb 16, 2004, in forum: Perl Misc
    Replies:
    4
    Views:
    174
    Matt Garrish
    Feb 16, 2004
  5. Isaac Won
    Replies:
    9
    Views:
    447
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page