I recently got absorbed by threads in Perl. Why? Well, because I'm working on a product - iSMS - that I hope to be of use to some with some money to spend, otherwise it'll go OSS as is its predesessor. Anyway
So I set off reading about and experimenting with the new Perl threading implementation. A search on Google for some 'prior art' on Perl threads gave me the following site that compares threading in different languages.
I just compiled 5.6.1 with -Duse5005threads and checked the Thread implementation with the threads implementation in 5.8.0, sorta
The following 'apps' were used:
---------------- Thread
#!/opt/perl561t/bin/perl
use strict;
use Thread qw(cond_wait cond_signal);
my $count = 0;
my $data = 0;
my $produced = 0;
my $consumed = 0;
sub consumer {
my $n = shift;
while (1) {
lock($count);
cond_wait($count) while ($count == 0);
my $i = $data;
$count = 0;
$consumed++;
last if ($i == $n);
cond_signal($count);
}
}
sub producer {
my $n = shift;
for (my $i=1; $inew(\&producer, $n);
my $c = Thread->new(\&consumer, $n);
$p->join;
$c->join;
print "$produced $consumed\n";
}
main;
---------------- threads
#!/opt/perl58/bin/perl
use strict;
use threads;
use threads::shared;
my $count : shared = 0;
my $data : shared = 0;
my $produced
my $consumed : shared = 0;
sub consumer {
my $n = shift;
while (1) {
lock($count);
cond_wait($count) while ($count == 0);
my $i = $data;
$count = 0;
$consumed++;
last if ($i == $n);
cond_signal($count);
}
}
sub producer {
my $n = shift;
for (my $i=1; $inew(\&producer, $n);
my $c = threads->new(\&consumer, $n);
$p->join;
$c->join;
print "$produced $consumed\n";
}
main;
---------------------------------------
I got the following test-bench results and was a bit astonished
> time
100000 100000
real 0m10.287s
user 0m5.360s
sys 0m4.930s
> time
100000 100000
real 0m15.660s
user 0m11.780s
sys 0m3.880s
Subsequent runs give the same result
I've done a multithreading daemon in Perl 5.5(something) around 2000 to 2001 and found it rather stable enough for production capabilities
Anyway
Hummm
Anyway
For the curious - and now I assume other people are actually reading this weblog - I'm using threading in my *product* in the following way
First a boss/worker kind of thingy, in which incoming requests - handled by the boss - are passed to a worker which will handle the request. This way we get a better response time...
Secondly a thread is launched at application start that implements a HTTP daemon. This daemon is used for configuration of the application. This thread implements an embedded web server used for handling the configuration
Aha
http://www.advogato.org/article/579.html
We'll we are at it, you can also look at another PIP initiative: http://patternsinperl.com/
Regards,
Johan
Perl threads getting slower? 0 Comments More | Login | Reply /