全部博文(1144)
分类: LINUX
2010-09-23 20:52:44
Perl has two thread models.
In Perl 5.005 the thread model was that all data is implicitly shared and shared access to data has to be explicitly synchronized. This model is called "5005threads".
In Perl 5.6 a new model was introduced in which all is was thread local and shared access to data has to be explicitly declared. This model is called "ithreads", for "interpreter threads".
In Perl 5.6 the ithreads model was not available as a public API, only as an internal API that was available for extension writers, and to implement fork() emulation on Win32 platforms.
In Perl 5.8 the ithreads model became available through the threads
module.
In Perl 5.10, the 5005threads model will be removed from the Perl interpreter.
Neither model is configured by default into Perl (except, as mentioned above, in Win32 ithreads are always available.) You can see your Perl's threading configuration by running perl -V
and looking for the use...threads variables, or inside script by use Config;
and testing for $Config{use5005threads}
and $Config{useithreads}
.
For old code and interim backwards compatibility, the Thread module has been reworked to function as a frontend for both 5005threads and ithreads.
Note that the compatibility is not complete: because the data sharing models are directly opposed, anything to do with data sharing has to be thought differently. With the ithreads you must explicitly share() variables between the threads.
For new code the use of the Thread
module is discouraged and the direct use of the threads
and threads::shared
modules is encouraged instead.
Finally, note that there are many known serious problems with the 5005threads, one of the least of which is that regular expression match variables like $1 are not threadsafe, that is, they easily get corrupted by competing threads. Other problems include more insidious data corruption and mysterious crashes. You are seriously urged to use ithreads instead.
Here is a example of using the Perl interpreter-based threads, it create 10 threads every time.
note:Perl 5.8.8 没有 is_joinable。可以用父子线程共享的线程个数计数器来控制子线程数量,然后在线程创建后,将其detach 。
is_joinable来控制子线程数量
---------------------------------------------------------------------------------------
#!/usr/bin/perl
use threads;
use strict;
use Data:Dumper;
sub prt(){
`echo "$_[0]" >> t.txt`;
}
my $current_thread;
my %thrs = ();
for(1..300)
{
$current_thread = scalar (keys %thrs);
if( $current_thread < 10 )
{
$thrs{$_} = threads->create(\&prt,$_);
}else{
print "more than 10 threads\n";
print Dumper \%thrs;
foreach my $t (keys %thrs){
if($thrs{$t}->is_joinable()){
$thrs{$t}->join();
delete $thrs{$t};
}
}
$thrs{$_} = threads->create(\&prt,$_);
}
}
foreach my $t (keys %thrs){
$thrs{$t}->join();
delete $thrs{$t};
}
print Dumper \%thrs;
用父子线程共享的线程个数计数器来控制子线程数量
---------------------------------------------------------------------------------------
#!/usr/bin/perl
use strict;
use warnings;
use threads;
use threads::shared;
my $thread_num :shared = 0;
sub start_thread {
print "Thread ", threads->tid(), " started, current thread numbers $_[0]\n"; #Code HERE,out of lock block
{
lock($thread_num);
$thread_num--;
cond_signal($thread_num);
}
}
for (1 .. 200) {
{
lock($thread_num);
cond_wait($thread_num) until $thread_num < 10;
my $t = threads->create(\&start_thread, $thread_num + 1);
$t->detach();
$thread_num++;
}
}
sleep(1);
使用 Thread::Semaphore
---------------------------------------------------------------------------------------
#!/usr/bin/perl
use strict;
use warnings;
use threads;
use Thread::Semaphore;
my $thread_num = Thread::Semaphore->new(10);
sub start_thread {
print "Thread ", threads->tid(), " started\n";
$thread_num->up();
}
for (1 .. 200) {
$thread_num->down(); #这行代码好像应该放到下面的if block里面。
if (my $t = threads->create(\&start_thread)) {
$t->detach();
}
}
sleep(2);