[THREADS] Behaviour of Thread#stop?

Discussion in 'Ruby' started by Xavier Noëlle, Dec 14, 2010.

  1. Hello dear Fellow Rubyists !
    I'm currently struggling with Ruby threads, especially the #stop and
    #stop? methods. My program has two threads:
    - a worker performing tasks and ultimately calling Thread.stop (it is
    supposed to do so before to be allowed to continue)
    - a monitor asking for the status of the worker thread and waiting
    until it has reached the call to Thread.stop

    I thought that Thread#stop? would be the way to go for the monitor
    thread, but it doesn't seem to do what I want. It doesn't work with
    1.9, but seems to work with 1.8, which is quite confusing.

    The piece of code trying to wake the worker up as follow:
    while !thr.stop?()
    Thread.pass()
    end
    thr.wakeup()

    Unfortunately, thr.stop?() never returns false, so I'm basically
    trying to wake a thread that is not sleeping !

    TIA ! :)

    --
    Xavier NOELLE
    Xavier Noëlle, Dec 14, 2010
    #1
    1. Advertising

  2. Re: Behaviour of Thread#stop?

    "Xavier No=C3=ABlle" <> wrote in post #968343:
    > I'm currently struggling with Ruby threads, especially the #stop and
    > #stop? methods.


    You probably don't want to be doing this. There are higher-level data =

    structures which will more likely do what you want.

    For example, look at the Queue class. Your worker(s) can pop a message =

    from the queue as its instructions to work. It works happily with =

    multiple workers popping from the same queue, as they'll take it in =

    turns to pop.

    Look also at ConditionVariable.

    Note you need to require 'thread' for both Queue and ConditionVariable.

    Also, I saw a simple Semaphore class posted a long time ago, which isn't =

    in the standard library, but you can just copy-paste it.

    -- =

    Posted via http://www.ruby-forum.com/.=
    Brian Candler, Dec 14, 2010
    #2
    1. Advertising

  3. Xavier Noëlle

    Su Zhang Guest

    Re: Behaviour of Thread#stop?

    thr.stop? returns true not only when thr is dead but also when it is
    sleeping. Moreover, if thr gets blocked outside by the global
    interpreter lock (i.e. not executing at the moment), then thr will also
    be put into a `sleep' state, and thus thr.stop? will return false.

    In your case, the tasks seem to be naturally cooperative, so a fiber is
    preferred to a thread.

    --
    Posted via http://www.ruby-forum.com/.
    Su Zhang, Dec 15, 2010
    #3
  4. Re: Behaviour of Thread#stop?

    2010/12/14 Brian Candler <>:
    > You probably don't want to be doing this. There are higher-level data
    > structures which will more likely do what you want.
    >
    > For example, look at the Queue class. Your worker(s) can pop a message
    > from the queue as its instructions to work. It works happily with
    > multiple workers popping from the same queue, as they'll take it in
    > turns to pop.


    You're absolutely right. I forgot about the Queue structure, which is
    perfect for my needs !

    > Also, I saw a simple Semaphore class posted a long time ago, which isn't
    > in the standard library, but you can just copy-paste it.


    I'll take a look at that. It would be nice if it was in the standard library !

    @Su Zhang: I tried with a dummy loop (in fact, there is no code
    between the mutex release and Thread.stop(), so I needed to cheat) and
    #stop? returns false even when not sleeping. What do you mean by
    "blocked outside by the GIL" (I know what the GIL is, but don't
    understand what kind of problem you're talking about in this
    sentence).

    --
    Xavier NOELLE
    Xavier Noëlle, Dec 16, 2010
    #4
    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. Matt Theule

    Stop Debugging doesn't stop in ASP.NET

    Matt Theule, Jul 23, 2003, in forum: ASP .Net
    Replies:
    7
    Views:
    713
    Matt Theule
    Jul 24, 2003
  2. Son KwonNam
    Replies:
    11
    Views:
    2,580
    mr_organic
    Apr 9, 2004
  3. Richard
    Replies:
    11
    Views:
    762
    Jesper Nordenberg
    May 4, 2004
  4. Benji
    Replies:
    34
    Views:
    1,160
    pkriens
    Oct 28, 2005
  5. Enigma Curry
    Replies:
    1
    Views:
    472
    Peter Hansen
    Mar 15, 2006
Loading...

Share This Page