threads::shared - ��鴻�㋘����������с����若�炊����������掩����������������� Perl �����壔�鴻����潟�激�с�
�����勉����㏍�ャ�<�潟����� threads::shared �����若�吾�с�� 1.14 ���荐�菴違����⓾����障�����
use threads;
use threads::shared;
my $var :shared;
$var = $scalar_value;
$var = $shared_ref_value;
$var = share($simple_unshared_ref_value);
my ($scalar, @array, %hash);
share($scalar);
share(@array);
share(%hash);
my $bar = &share([]);
$hash{bar} = &share({});
{ lock(%hash); ... }
cond_wait($scalar);
cond_timedwait($scalar, time() + 30);
cond_broadcast(@array);
cond_signal(%hash);
my $lockvar :shared;
# condition var != lock var
cond_wait($var, $lockvar);
cond_timedwait($var, time()+30, $lockvar);
��������í�˨����˨�������⓾��紊���違�壠����鴻�㋘��������絲障�������í�ゃ����若����с�� ��違����̥����������������鴻�㋘�������壔����с��絖���������⓾��������紊���違�勉����í�ゃ����若����ŝ�潟����若�� 緇���障����� �����勉�≪�吾�ャ�若�˨�壔����違�ŝ����鴻�㋘�����鐚���� Win32 筝���勖�㋙室 fork鐚������у����違�� ��掩������������������櫝�純�˨����障����� threads ��≪�吾�ャ�若�˨����宴�˩戎�����障�����
(�����壔�鴻����若��)
share
, cond_wait
, cond_timedwait
, cond_signal
, cond_broadcast
, is_shared
��障�� threads �����㏍�若����������⓾����ŝ��罧級����с����勉�≪�吾�ャ�若�˨����ゃ�潟����若�������������� �����勖����純�壠����☎�≦�鴻�˨�ŝ�����������羈���������⓾�������������� ��������˨�������鴻�㋘�������脂����鴻�㋘�������医����勌検��鴻�у��篏���������≪�吾�ャ�若�˨�� ��吾�������������с����障�����
(��∽��)
-
share
��壠�ゃ��綣���違����������������������掩�������������������勉�������⓾����若�壔����障����� ��鴻�˨�í�若�������������������激�ャ����鴻�˨�í�若�ŝ����<�㋘�潟�鴻����������ŝ����<�㋘�潟�鴻������������� ��������激�ャ�ŝ����<�㋘�潟�鴻����掩����������������������с����障�����share
��壠�掩���������������勈昇���(rvalue)���菴������障����������������壠幻�� ��ŝ����<�㋘�潟�鴻�������☀����������障�����:shared
絮���э��my $var : shared;
鐚����篏帥����������с��������紊���違�� ��潟�潟����ゃ����������掩�������������������勉�������⓾����若�壔����������������с����障�����Perl ��勉����㏍����帥�ゃ��絎h����˩軸�����馹���с����������違����̥�������������ŝ����<�㋘�潟�鴻�� ��掩�������������翫�����
&share([])
���&share({})
���������罕������� 篏帥��綽�荀������������障�������掩����������鴻�˨�í�˩撮��ャ�с�������ゃ�壔��篁���勉�鴻�˨�ï�ゃ�������掩�������� ��ŝ����<�㋘�潟�鴻�������с��:
my $var :shared; $var = 1; # ok $var = []; # error $var = &share([]); # ok
share
��壔�ŝ����<�㋘�潟�鴻��罩g∈��� 1 ��㋘����˨�����茯帥�鴻�障�����share(\$a)
���share($a)
��◐��箴<�с��������share(\\$a)
��奝�������障����� ��������壔�������鴻����������掩�������若�炊�������壔����障����������������勐�掩�������ŝ����吾�с�壔�����篏���������� ��������������������������掩����������激�ャ����≪�㋘�ゃ�̬申�����������������˨����c� 篏���������ŝ�������違�ŝ����ŝ����������������潟����障�����my %hash :shared; $hash{'meaning'} = &share([]); $hash{'meaning'}[0] = &share({}); $hash{'meaning'}[0]{'life'} = 42;
-
is_shared
��壔�����絎����������紊���違����掩����������⓾����������í�������������с����壔����障����� ��掩����������⓾�������違��(refaddr() ������罕����) 紊���違� ������ ID ���菴������障����� ��������с�ŝ�������違��undef
���菴������障�����if (is_shared($var)) { print("\$var is shared\n"); } else { print("\$var is not shared\n"); }
- lock VARIABLE
-
lock
��壔�鴻�潟�若��������紊���������障�у����違����㏍����壔����障����� ������篁���勉�鴻�㋘�������˨����c�⓾����勐����違����㏍����壔�������⓾�������ŝ�������㏍����壔�� �櫝�純�˨�ŝ����障��lock
��勐�若�喝�冴����壔����㏍����壔�������障����� ��������˨����鴻����������鴻�潟�若����勌賢��������������鴻�㋘�������˨����c� 茲���医��lock
�����若�喝�冴����⓾��絎������с�� -- ������紊���眼�勉�㏍����壔����鴻�潟�若�������� �����������障�с����勐����違�壔�㏍����壔�����膓������障�������������激�ャ����������������c����潟�潟�������ŝ����吾�с�壔�������㏍����壔����⓾����� �����勤��膣���壔�㏍����壔�������障�������� 箴������違����������鴻�㋘��������
lock(@a)
��������������⓾�����篁���勉�鴻�㋘�������勤�����lock($a[12])
��壔����㏍����壔�������障��������lock
��壩③腆冴�� 1 ��㋘����˨�ŝ����<�㋘�潟�鴻��莨帥����障�����lock(\$a)
��壔��lock($a)
��◐��箴<�с��������lock(\\$a)
����� 膈�箴<�с�壔�������障�����������腓榊�����紊���違�� unlock ��������������壔�с����ŝ�����������羈���������⓾�����������; ��㏍����壔����鴻�潟�若����������������勉��緇���ゃ�������������障�������� ��������壔����㏍����壔�勐����眼�у����違����㏍����壔�������������ф�����膂≦����̹�������с����障�����
my $var :shared; { lock($var); # $var is locked from here to the end of the block ... } # $var is now unlocked
��������掩��紊���違�吾�勉�≪�壔�祉�鴻�˨�ゃ����⓾�����膕上墾��勰�������九勝�����������ŝ����� Thread::Semaphore ��������с����⓾��������������
- cond_wait VARIABLE
- cond_wait CONDVAR, LOCKVAR
-
cond_wait
��∽�違�� ��㏍����壔�������� 紊���違��綣���違����������������勐����違�勉�㏍����壔�� 茹i�ゃ����障����� ��������☖����勉�鴻�㋘�������������勐�������㏍����壔�������⓾�����紊���違����������cond_signal
���cond_broadcast
��������障�с�������㏍����姒��緇�罘�鐚������障�����cond_wait
��������㏍����壔�����紊���違�壔��cond_wait
���絎�篋�������緇�������綺 ��㏍����壔�������障����� ������茲���違�勉�鴻�㋘��������������紊���違��絲障�����cond_wait
�����⓾�������ŝ����� 筝���ゃ����ゃ����☗����⓾�勉�鴻�㋘����������㏍����壔����峨����������障�у��罘�������������������綺 �����㏍����壔����障�� (�����c�☗�������勉��������cond_wait
���篏帥����������ŝ������櫝�純�ʒ����� ��í����㏍����壔��茹i�ゃ����⓾�����������)��� 紊���違�勉�㏍����虁В��ゃ����������㏍����壔�������☗����∞�倶�������ャ�����������篋���ゃ�勐��篏��� ��≪����������壔�с����� 緇���∞�倶�����������������������������紊���違�勐����㏍����壔��������篋���ゃ�勐��篏���壔�� ��≪����������壔�с�壔�������障��������膃㋙����勖�後����с�壔��
cond_wait
��� ��㏍����壔�������⓾����ŝ�� ��掩��紊���違����������� �����勐�������� ��㏍����� �����������掩��紊���違�������障����� �����勌����ɾ�勉�勐����違�壔�㏍����壔��茹i�ゃ����������������☖����勉�鴻�㋘��������筝���ɾ�勉�勐����違� ��激�違����˨����������障�с�������勉�鴻�㋘�������勐��茵���壠��罩≪����障�������í�勉�鴻�㋘�����������紊���違��絲障��
cond_signal
���cond_broadcast
��������ŝ����⓾����� �����勐����違�� notify ������������������������������羈����������������������荀���с����� ���������������紊���違�勐�ゃ�勉����с����壠����潟��荀�羆����羣������������ŝ����翫�����緇���∞�倶���� ��祉��������������荀���с����� 箴������違����掩����˨�⓾�潟�帥�� 0 ��˨�ŝ����障�у��罩≪�������˨��:{ lock($counter); cond_wait($count) until $counter == 0; }
- cond_timedwait VARIABLE, ABS_TIMEOUT
- cond_timedwait CONDVAR, ABS_TIMEOUT, LOCKVAR
-
篋���ゃ�勐����違��������綵√����с�壔��
cond_timedwait
��� ��㏍����壔�������� 紊���違� 腟九嚳�����ŝ�帥�ゃ����≪�⓾�����������綣���違�˨�������障����� 紊���違�壔�㏍����虁В��ゃ����������帥�ゃ����≪�⓾����������̹��������������篁���勉�鴻�㋘��������紊���違� ��激�違����˨�������������������障�с����㏍����壔�������障����� ��帥�ゃ����≪�⓾����˨�ŝ�������純�勐�ゃ��菴���������障����� ��������с�ŝ�������亥����勐�ゃ��菴���������障����� ��í�<����勐�翫����с����祉����勰�����紊���違�壠����㏍����壔�������障�����cond_wait
���罕���������勰�∽�違�� ��㏍����壔�������� ��掩��紊���違��菴遵���� �����í�<�若�帥�������⓾�������障��; �����勐�翫�������������勉����í�<�若�帥� ��㏍����壔�������⓾����ŝ�� ��>散紊���違�с�������������������阪�ャ����������㏍����壠����違�˨����c� 絎���������障��������������
cond_wait
���罕����荀���������㏍����壔�勐����峨����壔�≪����������壔�с�壔�������障�������� �����勰�∽�違����祉�c��緇���������ŝ����������������倶����˨�ŝ�c�⓾����������í�����絽吾� �����с����壔�������鴻����с����� ��������������帥�ゃ����≪�⓾����夌偽絲丞����ɱ�ゃ�ŝ�勉�с�������鴻������� ���荐�膊������������鴻����с�壔�������障�����:lock($var); my $abs = time() + 15; until ($ok = desired_condition($var)) { last if !cond_timedwait($var, $abs); } # we got it if $ok, otherwise we timed out!
- cond_signal VARIABLE
-
cond_signal
��∽�違�� ��㏍����壔�������� 紊���違��綣���違�˨�������������勐����違��絲障����cond_wait
�����⓾�����筝���ゃ�勉�鴻�㋘�������勉����㏍����壔��茹i�ゃ����障����� ������筝���や札筝���勉�鴻�㋘��������cond_wait
�����⓾����㏍����壔�������⓾�������ŝ����� ������筝���ゃ�勉�鴻�㋘�������������������㏍����壔��茹i�ゃ�������障�� (��������⓾�í�勌����ゃ���� 筝�腆阪����с��)�����������í�勉�鴻�㋘�������������勐����違��
cond_wait
�����⓾����ŝ����翫�������激�違����˨� ��贋����������障����� 絽吾�˨�激�違����˨�勐����˨�㏍����壔����������勉�с��篁���勉�鴻�㋘�������� cond_wait() �� ��ャ�������˨�激�違����˨����冴�������勉��鐚�羈���闋宴����������逸�������帥����������������堺�ャ�障�������㏍����壔�������⓾����ŝ��紊���違��絲障��
cond_signal
���荅⓾�帥�����������絽吾�虁⑥������ ��冴����障����� 腮���̬儀�����������勤⑥��������������������翫�����罨<�勖�号����ц⑥�����������吟����������������с����障��:{ no warnings 'threads'; cond_signal($foo); }
- cond_broadcast VARIABLE
-
cond_broadcast
��∽�違��cond_signal
��������鋍若����������������障����� ���������cond_broadcast
��壔�鴻�㋘�����筝���ゃ�������с�壔�ŝ�������㏍����壔��������紊���違� 絲障�����cond_wait
�����☗��罘�筝㏍�� �����⓾�� ��鴻�㋘�������������㏍����虁В��ゃ����障�����
threads::shared bless �����鴻�㋘����������障�������т����㋘������������� ��掩����ŝ����吾�с�壔����у��篏������������若�吾�с�潟�� bless() ��� �����壔�鴻����若�������障�����
# Create a shared 'foo' object
my $foo;
share($foo);
$foo = &share({});
bless($foo, 'foo');
# Create a shared 'bar' object
my $bar;
share($bar);
$bar = &share({});
bless($bar, 'bar');
# Put 'bar' inside 'foo'
$foo->{'bar'} = $bar;
# Rebless the objects via a thread
threads->create(sub {
# Rebless the outer object
bless($foo, 'yin');
# Cannot directly rebless the inner object
#bless($foo->{'bar'}, 'yang');
# Retrieve and rebless the inner object
my $obj = $foo->{'bar'};
bless($obj, 'yang');
$foo->{'bar'} = $obj;
})->join();
print(ref($foo), "\n"); # Prints 'yin'
print(ref($foo->{'bar'}), "\n"); # Prints 'yang'
print(ref($bar), "\n"); # Also prints 'yang'
(羈���
threads �����ñ����с����ŝ����翫�����threads::shared ��奝����c�☗�ñ��筝��檽�˨�ŝ�������� 荐㊧����������⓾����障����� threads ��˨�≪�壔�祉�鴻�������������������ŝ�����use threads::shared
����������� use threads
�����ŝ�������違�ŝ����障�������� threads::shared ��勐����� threads ��� use ��������������������違�� 茘☗�������冴����������障�����
(�����違����狗��)
share
��������������������激�ャ����������ŝ����<�㋘�潟�鴻����������激�ャ�ŝ����<�㋘�潟�鴻� 篏帥����������������˨�障����������������������若�帥�壠け��������障�����
my @arr = qw(foo bar baz);
share(@arr);
# @arr is now empty (i.e., == ());
# Create a 'foo' object
my $foo = { 'data' => 99 };
bless($foo, 'foo');
# Share the object
share($foo); # Contents are now wiped out
print("ERROR: \$foo is empty\n")
if (! exists($foo->{'data'}));
緇���c�⓾�������勉�������ɱ����違�壔����掩�����������絎h�������� 緇���� 荐㊤�������⓾�������������� (��鴻�˨�í����鴻�˨�í�ŝ����<�㋘�潟�鴻�壔����勐�馹���勐襲��帥����������障��������)
��壔�í�壕�ʃ昆�����掩�����絲上�������������������吾�������⓾����ŝ����ŝ����吾�с�壔�������掩���������������� ��������⓾��莖∽����ŝ����������虁�������障�������� 箴������違�������鴻�㋘�������勉�鴻�潟�若��������紊�������罸���˨����������ŝ����吾�с�壔���� �����鴻����í�壔�帥��茲���医����若�喝�冴����������������ャ����障�������� ������筝���ゃ�勐�演�堺�с�壔����������激�ャ����若�鴻�勉�ŝ����吾�с�壔����勐��絎鴻����� 筝�菴違�������狗����勉��������紊宴�������⓾����障����������с����� ��ŝ����吾�с�壔����掩�����絲上����������壔�í�鴻�勌�������鴻�˨�ゃ����⓾�壔��(�����勉�≪�吾�ャ�若�˨� CPAN ���絽������˨�障����⓾����� examples/class.pl ��������с����⓾��������������
���������絲障����� splice
��壔�泣����若�������⓾����障�����!
��掩�������������������������������激�ャ�勤��膣���吾�勉�ŝ����<�㋘�潟�鴻�������c�⓾�� ��ɱ�������劫�������障�������� ��障�������掩��������鐚���������激�ャ�у����������ŝ����ゃ�潟�������壔�鴻�� ��㏍�若�˨�鴻�í�ゃ�鴻����⓾����������勤��膣���虁�ɱ�������劫�������障��������
share($hashref->{key})
�����⓾�����share()
��壔����í�若�<����祉�若�吾�� ��冴����障�������� ������������$hashref->{key}
��壠�掩�� ��������障�������� lock($hashref->{key})
�������������������� "locking can only be used on shared values" (��㏍����壔�壠�掩��紊���違��絲障����⓾�勉�推戎�����с����障��) ��������������í�若����榊�������障�����
��≪����怨����������⓾����������違�勰�画Η��������違����������違����馹���������������ŝ�í�勖��腮水��: http://rt.cpan.org/NoAuth/Bugs.html?Dist=threads-shared
CPAN ��� threads::shared �����c�鴻�˨����激�с�潟����í�若�í��: http://www.cpanforum.com/dist/threads-shared
threads::shared ��勖絵���篁���� POD: http://annocpan.org/~JDHEDDEN/threads-shared-1.14/shared.pm
��純�若�鴻�㋘����吾�����: http://code.google.com/p/threads-shared/
http://www.perl.com/pub/a/2002/06/11/threads.html �� http://www.perl.com/pub/a/2002/09/04/threads.html
Perl threads ��<�若�ŝ�潟�違�ŝ�鴻��: http://lists.cpan.org/showlist.cgi?name=iThreads
Artur Bergman <sky AT crucially DOT net>
threads::shared is released under the same license as Perl.
Documentation borrowed from the old Thread.pm.
CPAN version produced by Jerry D. Hedden <jdhedden AT cpan DOT org>.