The Computer Language
Benchmarks Game

thread-ring Perl program

source code

# The Computer Language Benchmarks Game
# http://benchmarksgame.alioth.debian.org/

#  contributed by Richard Cottrill


use strict;
use warnings;
use threads;
use threads::shared;
use Thread::Semaphore;

my $numThreads	:shared;
my $data	:shared;
my $result	:shared;
my @mutex	:shared;

$numThreads = 503;

sub thr_func {
  my ($nextThread, $thr_name);
  $thr_name = threads->tid();
  threads->detach();
  if ($thr_name == $numThreads) {
    $nextThread = 1;
  }
  else {
    $nextThread = $thr_name + 1;
  }
  while (1) {
    $mutex[$thr_name]->down();
    if ($data) {
      $data = --$data;
      $mutex[$nextThread]->up();
    }
    else {
      $result = $thr_name;
      $mutex[0]->up();
    }
  } 
}

$data = $ARGV[0];

$mutex[0] = new Thread::Semaphore(0);
{
  for (1 .. $numThreads) {
    $mutex[$_] = new Thread::Semaphore(0);
    threads->create(\&thr_func);
  }
}
$mutex[1]->up();
$mutex[0]->down();
print "$result\n";
exit(0);
    

notes, command-line, and program output

NOTES:
32-bit Ubuntu one core
This is perl 5, version 24, subversion 0 (v5.24.0) built for i686-linux


Tue, 10 May 2016 20:22:04 GMT

COMMAND LINE:
/usr/local/src/perl-5.24.0_no_ithreads_no_multi/bin/perl threadring.perl 500000

PROGRAM FAILED 


PROGRAM OUTPUT:

This Perl not built to support threads
Compilation failed in require at threadring.perl line 9.
BEGIN failed--compilation aborted at threadring.perl line 9.