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:
64-bit Ubuntu quad core
This is perl 5, version 26, subversion 0 (v5.26.0) built for x86_64-linux-gnu-thread-multi
Thu, 16 Nov 2017 21:37:54 GMT
COMMAND LINE:
/usr/bin/perl threadring.perl 50000000
PROGRAM OUTPUT:
292