Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > [THREADS] Behaviour of Thread#stop?

Reply
Thread Tools

[THREADS] Behaviour of Thread#stop?

 
 
Xavier NoŽlle
Guest
Posts: n/a
 
      12-14-2010
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

 
Reply With Quote
 
 
 
 
Brian Candler
Guest
Posts: n/a
 
      12-14-2010
"Xavier No=C3=ABlle" <(E-Mail Removed)> 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/.=

 
Reply With Quote
 
 
 
 
Su Zhang
Guest
Posts: n/a
 
      12-15-2010
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/.

 
Reply With Quote
 
Xavier NoŽlle
Guest
Posts: n/a
 
      12-16-2010
2010/12/14 Brian Candler <(E-Mail Removed)>:
> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
debugger behaviour different to execution behaviour Andy Chambers Java 1 05-14-2007 09:51 AM
Strange mouse behaviour with flash in Mozilla 1.3.7 hpoppe Firefox 0 11-07-2004 12:16 PM
Strange taskbar behaviour (notification area) Falcon Wireless Networking 0 08-17-2004 09:03 AM
[mozilla1.6] strange behaviour with newsgroup joost68 Firefox 5 04-03-2004 03:48 AM
A curious behaviour with an URL on the W3C Griffure Firefox 0 08-11-2003 11:46 AM



Advertisments