use Tie::File; use threads; use strict; use constant NUMBER_OF_THREAD => 10; $|++; open(ACCOUNT, ">", "account.txt"); foreach (0..NUMBER_OF_THREAD) { print ACCOUNT "1000\n"; } close(ACCOUNT); tie my @account, "Tie::File", "account.txt" or die "cannot tie\n"; foreach (1..NUMBER_OF_THREAD) { my $kid = threads->create(\&thread_job, $_); $kid->detach(); } untie @account; sub thread_job { my $id = shift(); print "I am a child, this is the name my parents gave me: ", $id, +"\n"; while (1) { $account[$id] -= 100; $account[$id] += 100; } }
This post is for a general purpose, and it is not about Tie::File. Tie::File is just an example, and is the point where I started to think about this.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Perl should start to specify which function and module are thread-safe
by integral (Hermit) on Feb 02, 2003 at 19:58 UTC | |
by pg (Canon) on Feb 02, 2003 at 20:24 UTC | |
by integral (Hermit) on Feb 02, 2003 at 20:32 UTC | |
Re: Perl should start to specify which functions and modules are thread-safe
by zentara (Cardinal) on Feb 03, 2003 at 15:37 UTC |