while(1) and threads

Discussion in 'Ruby' started by Paul, Jun 17, 2004.

  1. Paul

    Paul Guest

    I have many threads that I leave running, doing things in the
    background. I end up writing a lot of code like the following - is
    there a way to make a thread run, avoiding the while(1) ?

    Thanks

    Paul

    a = Thread.new(){
    aa = 0
    while(1)
    puts "a thread: #{aa}"
    aa=aa+1
    end
    }

    b = Thread.new(){
    bb = 0
    while(1)
    puts "b thread: #{bb}"
    bb=bb+2
    end
    }

    a.join
     
    Paul, Jun 17, 2004
    #1
    1. Advertisements

  2. Paul

    Kirk Haines Guest

    Do you just want a different looping construct?

    a = Thread.new {
    aa = 0
    loop do
    puts "a thread: #{aa}"
    aa += 1
    end
    }


    Kirk Haines
     
    Kirk Haines, Jun 17, 2004
    #2
    1. Advertisements

  3. Paul

    Gennady Guest

    If you need another way to make a loop, here it is:

    loop do
    end

    If you want to avoid a loop altogether, then I do not understand what
    you mean. After all a "thread" is a "thread of execution" -- execution
    stops, the thread stops.
     
    Gennady, Jun 17, 2004
    #3
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.