multi-threaded Perl, I've missed you...

The blog is currently being ported from WordPress to over 12 years of static pages of content. If there's an article missing that you're hoping to see, please contact me and let me know and I'll prioritize getting it online.

June 30, 2010


When I was a senior engineer at The Rubicon Project, I inherited some Perl code to analyze log data for online advertising impressions. It eventually hit a maximum on the multi-core systems I had at my disposal and so rewrote my code to use threads. In Perl. Yes, you read that correctly.

It was actually one of my prouder engineering feats in my career, to scale some software that could analyze a few million ad impressions per day to scale up to handling more than a billion data points per day.

I left Rubicon at the end of 2009, suffering serious burnout and at the next job I had (Armor Games), I had the need to work with Perl again for something, and wrote up this little example of using Perl threads more for my own benefit.

#!/usr/bin/perl -w
use strict ;
my $can_use_threads = eval 'use threads; 1';
die("Your Perl doesn't support threads\n") if (!$can_use_threads) ;

use threads ;
use threads::shared ;

my @threads = () ;

# spin a bunch of threads
for (my $i=0; $i<5; $i++) {
  my $param1 = $i ;
  my $param2 = $i**2 ;
  $threads[$i] = threads->create(&thread_function, $param1, $param2) or die("couldn't create a thread for i: $i\n") ;
}

# wait for all threads to finish
for (my $i=0; $i<5; $i++) {
  $threads[$i]->join() ;
}

exit ;

sub thread_function
{
  my $param1 = shift ;
  my $param2 = shift ;
  print "Hi there, I'm thread #$param1, my value is $param2\n" ;
}