Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Running perl threads in loop

by dvinay (Acolyte)
on Sep 15, 2013 at 14:00 UTC ( [id://1054183]=perlquestion: print w/replies, xml ) Need Help??

dvinay has asked for the wisdom of the Perl Monks concerning the following question:

hi,

I want to run perl threads in an For loop, for example suppose, i create 2 threads each containing an command to execute, once these 2 threads are completed i need to another set of same 2 threads and run it, and this should go on for the number of times i have given as an input to the main script below is the small code snippet

my @threads; #---Making Concurrent OneClick backup commands using threads------#
for (my $i=0;$i<=3;$i++) { #-----performing the CHO loops as given by user-------# foreach (@finalOneClickarray) { push @threads, threads->new(\&concurrentBackupCommandsR +ead, $_); } foreach (@threads) { $_->join(); } } sub concurrentBackupCommandsRead() { doing my stuff here......... }

i have tried to put for loop, but it errors out saying Perl exited with active threads: please let me know what am i doing wrong here

Replies are listed 'Best First'.
Re: Running perl threads in loop
by NetWallah (Canon) on Sep 15, 2013 at 16:43 UTC
    Here is sample working code showing how your work could be implemented (based on my understanding of your text):
    #!/usr/bin/perl use strict; use warnings; use threads; #use threads::shared; print "Starting main program\n"; my $MAXTHREADS=5; my @work= map{int(rand()*1000)} 1..rand()*200; print "Work to do: " , scalar(@work) , " entries:\n\t@work\n\n"; my (@threads); my $workstart= 0; my $workincrement = int((scalar(@work) + $MAXTHREADS - 1) / $MAXTHREA +DS ); for my $CurrentThreadNumber (1..$MAXTHREADS) { my $t = threads->new(\&sub1, $CurrentThreadNumber, $workstart, + $workstart+$workincrement); $workstart +=$workincrement+1; push(@threads,$t); } foreach (@threads) { $_->join; } print "End of main program\n"; sub sub1 { my ($thn,$workstart, $workend)=@_; $workend =$#work if $workend > $#work; print "in Thread $thn starting $workstart Till $workend\n"; print "($thn: $work[$_]) " for $workstart..$workend; print "\n"; sleep rand() * 10; print "Exiting Thread $thn\n"; }
    Update: Handling the edge case where scalar(@work) <= $MAXTHREADS is left as an exercise.

                 My goal ... to kill off the slow brain cells that are holding me back from synergizing my knowledge of vertically integrated mobile platforms in local cloud-based content management system datafication.

Re: Running perl threads in loop
by BrowserUk (Patriarch) on Sep 15, 2013 at 14:47 UTC
    please let me know what am i doing wrong here

    One thing you are doing wrong is that @threads will retain the handles from threads created during the first iteration of the loop, for every subsequent iteration.

    Try moving its declaration inside the loop.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Running perl threads in loop
by CountZero (Bishop) on Sep 15, 2013 at 14:32 UTC
    Please put your code in <code> ... </code>-tags. That makes it much easier to read your code and test it.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics

      Sorry for my mistake, i know i haven't followed the rules, now i have updated my code snippet, please let me know what is that wrong i am doing here

      Thanks, Vinay
Re: Running perl threads in loop
by Anonymous Monk on Sep 16, 2013 at 12:10 UTC
    A much better design is to spawn threads which accept requests from some sort of queue and process them repeatedly ... remaining alive the entire time. A thread is not a subroutine, even though it runs one.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-23 16:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found