Professional Web Applications Themes

Using threads to obtain a value - Ruby

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

  1. #1

    Default Using threads to obtain a value

    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 doentation 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.
    Zachary P. Landau Guest

  2. #2

    Default Re: Using threads to obtain a value


    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" <kapheinehypa.net> schrieb im Newsbeitrag
    news:e81e52a8.0307151130.5bd9ab91posting.google.c om...
    > 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 doentation 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.
    Robert Klemme Guest

  3. #3

    Default Re: Using threads to obtain a value

    "Kurt M. Dresner" <kdresnercs.utexas.edu> wrote in message news:<20030716172538.GA15712cs.utexas.edu>...
    > If the computation is just raw computation, I don't think you're going
    > to see very much of a speedup by doing them in parallel. If they are
    > blocking for I/O or something like that at all, then there would be an
    > improvement, but otherwise not. Where is fetchdata getting the
    > information from? A remote source?
    Yeah, it is fetching the information from webpages. Most of the code
    other than waiting for pages to load is simple regexps, so I figured
    this would be the perfect use for threads.
    Zachary P. Landau Guest

Similar Threads

  1. Why are these threads deleted so often?
    By VeganMan007 in forum Macromedia Exchange Dreamweaver Extensions
    Replies: 1
    Last Post: September 8th, 09:13 PM
  2. threads in perl
    By Igor Ryaboy in forum PERL Beginners
    Replies: 24
    Last Post: March 3rd, 06:59 AM
  3. Objects, threads and so on
    By Lists Perl Org in forum PERL Beginners
    Replies: 6
    Last Post: October 2nd, 06:20 AM
  4. Threads troubles
    By Stefan Weiss in forum PERL Miscellaneous
    Replies: 3
    Last Post: September 9th, 11:05 AM
  5. purpose of threads?
    By Paul Archer in forum PERL Miscellaneous
    Replies: 0
    Last Post: July 9th, 02:35 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139