Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multithreading a foreach loop with Threads 1

Status
Not open for further replies.

czarj

Technical User
Joined
Apr 22, 2004
Messages
130
Location
US
Is there any easy way to use the script syntax below with "Threads" to create a multithreaded application. Very simply I want to this entire block to be run with each ARGV specified at the command line simultaneously.

Code:
#!/usr/bin/perl
@filers = @ARGV;
if( @ARGV < 1) 
   {    
      print "usage: ./filersetup.pl filerIP-1 filerIP-2 ... filerIP-n\n";
	  exit 0;
   }

foreach $filer (@filers)  {		
   &aggr_create;
   &license_add;
   &rep_throttle;
   &opts_enable;
   &vol_create;
   &vol_options;
   &exports
}

--- You must not fight too often with one enemy, or you will teach him all your tricks of war.
 
Code:
#!/usr/bin/perl

use threads;

@filers = @ARGV;
if( @ARGV < 1)
   {    
      print "usage: ./filersetup.pl filerIP-1 filerIP-2 ... filerIP-n\n";
      exit 0;
   }

# keep array of threads if you want to gracefully exit the
# app by waiting for them all to finish
my @threads = ();

foreach $filer (@filers)  {
   push (@threads, threads->create (\&thread_func, $filer));
}

# wait for all the threads to finish before terminating
# the parent.. otherwise terminating the parent force kills
# all the threads
foreach (@threads) {
   $_->join(); # blocks until this thread exits
}
exit(0);

# this is the main sub where all the threads start
sub thread_func {
   my $filer = shift;
   &aggr_create;
   &license_add;
   &rep_throttle;
   &opts_enable;
   &vol_create;
   &vol_options;
   &exports
}

Kirsle.net | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top