[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Using threads to obtain a value

kapheine

7/15/2003 7:31:00 PM

I have a few classes that all try to obtain the same data as each
other using different methods. Each one takes around 5 seconds to get
the data, so I'd much rather have them all try to get it at once. I
only need the data once, from whoever gets it first.

So what I would like to do is have, lets say, fetchdata() for each
class called in a thread, and as soon as the first non-nil is returned
from fetchdata, I want to kill the other threads and go on with the
program.

I have read the threading documentation and tried to play around a bit
with it, but I can't seem to get it to do what I want. If someone
could tell me how to do it or give me a point in the right direction,
that'd be helpful.
1 Answer

Robert Klemme

7/16/2003 9:03:00 AM

0


My first approach would be this: Create a condition variable and a mutex,
start all threads giving them the condition var and mutex, wait on the
condition var in the main thread. In all threads if the value is
retrieved signal the condition var. In the main thread kill all non
terminated threads (or simply let them run) and proceed.

robert



"Zachary P. Landau" <kapheine@hypa.net> schrieb im Newsbeitrag
news:e81e52a8.0307151130.5bd9ab91@posting.google.com...
> I have a few classes that all try to obtain the same data as each
> other using different methods. Each one takes around 5 seconds to get
> the data, so I''d much rather have them all try to get it at once. I
> only need the data once, from whoever gets it first.
>
> So what I would like to do is have, lets say, fetchdata() for each
> class called in a thread, and as soon as the first non-nil is returned
> from fetchdata, I want to kill the other threads and go on with the
> program.
>
> I have read the threading documentation and tried to play around a bit
> with it, but I can''t seem to get it to do what I want. If someone
> could tell me how to do it or give me a point in the right direction,
> that''d be helpful.