目次

背景?

Perl 5.6系以降のithreadsを使ってなかったので、暇つぶし?に書いてみた。

動くもの

動くものをみれば、大体なにするかはわかりそう・・と言うことで。^^;
#!/usr/local/bin/perl
use strict;
use threads;
use threads::shared;
use Time::HiRes qw( usleep );

my $foo : shared = 1;
my $bar = 1;
my %hash : shared = [];
my ($hoge, $i, $thrhash);
my $thrnum = 2;

my @thrs = ();

for ( $i = 0; $i < $thrnum; $i++ ) {
	# 無名のハッシュは {}
	$thrhash = {id=>$i, thr=>threads->new(\&sub1, $i)};
	push(@thrs, $thrhash);
}

foreach $thrhash ( @thrs ) {
	printf("joined thr id = %d [ret] %d\n", 
		$thrhash->{"id"}, $thrhash->{"thr"}->join);
}

printf("In the main[foo] %d [bar] %d\n",
	$foo, $bar);
printf("[hoge] %s[hash] %s\n",$hoge ,$hash{$hoge});

### sub routines ###
sub sub1 {
	my $id = $_[0];
	my $hoge = 1;
	$hash{$hoge} = "hoge$id";

	my $i;
	for ( $i = 0; $i < 10; $i++ ) {
		{
			lock($foo);# この{}内でロックが有効
			$foo += 1;
		}
		$bar += 1;
		printf("[id: %d]In the child [foo] %d [bar] %d\n",
			$id, $foo, $bar);
		printf("[hoge] %s[hash] %s\n",$hoge ,$hash{$hoge});
		usleep(100);
	}
}

実行環境(perl -v)

This is perl, v5.8.4 built for sun4-solaris-thread-multi

Copyright 1987-2004, Larry Wall

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on
this system using `man perl' or `perldoc perl'.  If you have access to the
Internet, point your browser at http://www.perl.com/, the Perl Home Page.

実行環境(perl -V)

Summary of my perl5 (revision 5 version 8 subversion 4) configuration:
  Platform:
    osname=solaris, osvers=2.9, archname=sun4-solaris-thread-multi
    uname='sunos mage 5.9 generic_112233-08 sun4u sparc sunw,ultra-5_10 '
    config_args=''
    hint=recommended, useposix=true, d_sigaction=define
    usethreads=define use5005threads=undef useithreads=define usemultiplicity=define
    useperlio=define d_sfio=undef uselargefiles=define usesocks=undef
    use64bitint=undef use64bitall=undef uselongdouble=undef
    usemymalloc=n, bincompat5005=undef
  Compiler:
    cc='gcc', ccflags ='-D_REENTRANT -fno-strict-aliasing -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64',
    optimize='-O2',
    cppflags='-D_REENTRANT -fno-strict-aliasing -I/usr/local/include'
    ccversion='', gccversion='3.3', gccosandvers='solaris2.9'
    intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=4321
    d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=16
    ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='off_t', lseeksize=8
    alignbytes=8, prototype=define
  Linker and Libraries:
    ld='gcc', ldflags =' -L/usr/local/lib '
    libpth=/usr/local/lib /usr/lib /usr/ccs/lib /usr/sfw/lib /usr/openwin/lib /usr/dt/lib /usr/local/ssl/lib
    libs=-lsocket -lnsl -ldl -lm -lpthread -lc
    perllibs=-lsocket -lnsl -ldl -lm -lpthread -lc
    libc=/lib/libc.so, so=so, useshrplib=true, libperl=libperl.so
    gnulibc_version=''
  Dynamic Linking:
    dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='  -R /usr/local/lib/perl5/5.8.4/sun4-solaris-thread-multi/CORE'
    cccdlflags='-fPIC', lddlflags='-G -L/usr/local/lib'


Characteristics of this binary (from libperl): 
  Compile-time options: MULTIPLICITY USE_ITHREADS USE_LARGE_FILES PERL_IMPLICIT_CONTEXT
  Built under solaris
  Compiled at Jun  4 2004 12:45:28
  @INC:
    /usr/local/lib/perl5/5.8.4/sun4-solaris-thread-multi
    /usr/local/lib/perl5/5.8.4
    /usr/local/lib/perl5/site_perl/5.8.4/sun4-solaris-thread-multi
    /usr/local/lib/perl5/site_perl/5.8.4
    /usr/local/lib/perl5/site_perl/5.8.3
    /usr/local/lib/perl5/site_perl
    .

Last-modified: 2006-02-01 18:33:37