develooper Front page | perl.ithreads | Postings from November 2011

Re: timing out threads

Thread Previous
From:
Francesco Nidito
Date:
November 2, 2011 05:05
Subject:
Re: timing out threads
Message ID:
CANn_qE6SmG5s5PyeWYnQz0DOtrczU7nNMxJQ+2N0po-O9xG44w@mail.gmail.com
Hi Danny,

You can just add a counter for each thread and propagate the counter to the
main thread at the end of the computation.

For instance, a hash local to each thread mapping from "job id" (in your
case the name of the file) and the time. At the end of the execution, you
can send the counters back to the main thread though a Queue and let the
main thread do some statistics and output the results.

I pseudocode:

task queue Q;
counters queue C;

thread {
   create counter c_X;

   while there is "something" in Q
       do "something";
       c_X{something} = time required to do "something";


   enqueue c_X in C;
}

main {
  create N threads;
  load Q with the tasks to be done;
  wait the N thread to finish (join);

  get all the counters c_i  from C;
  do something with c_1 ... c_N
}

Hope this helps!

Cheers,
nids

 ------------------------------
«Fear is not what's important; it's how you deal with it. It would be like
asking a marathon runner if they feel pain. It's not a matter of whether
you feel it; it's how you manage it.»
— James Nachtwey



On Tue, Nov 1, 2011 at 11:09 PM, Danny Wong (dannwong)
<dannwong@cisco.com>wrote:

> Hi Thread GURU,
>        I would like to perform the following, start X (say 10) number
> of threads to perform some operation (ex. Copy command), I would like to
> exit or return out of the thread(s) that takes too long ( ex. 30 minutes
> ). I'm thinking I should spawn an independent thread (a watch thread)
> and have that thread watch to see if any of the 10 threads are taking
> longer than 30 minutes to complete. Any example how I can accomplish
> this? Any other suggesting would be great to.
>
> BTW, I'm using perl 5.8.8., thread version 1.07.
>
> Thanks.
>

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About