Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^3: How to make a thread to wait till a command on shell get completed.

by Anonymous Monk
on Dec 03, 2013 at 08:34 UTC ( #1065390=note: print w/replies, xml ) Need Help??


in reply to Re^2: How to make a thread to wait till a command on shell get completed.
in thread How to make a thread to wait till a command on shell get completed.

You're loading Thread::Queue but not using it. You can much simplify your thread-creation logic with it:
sub Create { my ( $sourcePath, $destinationPath ) = @_; # ... create directories and stuff ... my $tq = Thread::Queue->new(); for (1..5) { push @arrThreads, threads->create( sub { while (defined(my $item = $q->dequeue())) { DoSomethingWithItem($item, $sourcePath, $destinati +onPath); } } ); } while (my ($k, $v) = each(%hashFileList)) { $tq->enqueue( $v ); } $tq->end(); foreach (@arrThreads) { my $num = $_->join; } }

The major change here is that it's passing the files one item at a time to the threads. You're dividing them to n buckets and passing the whole bucket to the thread at creation time.

Apart from being nicer to look at, this one-at-a-time (supervisor-worker) approach should ensure that the threads will finish quicker since they're given equal amounts of work.

  • Comment on Re^3: How to make a thread to wait till a command on shell get completed.
  • Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1065390]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2023-12-10 20:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?











    Results (41 votes). Check out past polls.

    Notices?