gulden has asked for the wisdom of the Perl Monks concerning the following question:
How can I share among threads a FileHandle to Write ?
#!/opt/coolstack/bin/perl -w use strict; use threads; use threads::shared; use FileHandle; my $nthreads = 64; my $x : shared; # For sync my $fh = FileHandle->new("> out.txt"); my @thrs; for(1..$nthreads){ print "START $_ \n"; my ($thr) = threads->create(\&worker, $_, $fh); push @thrs ,$thr; } $_->join for @thrs; print "STOP: " . localtime() . "\n"; $fh->close; print `wc -l out.txt`; exit; #---------------------------------------------------- sub worker{ my $id = shift; my $fh = shift; my $tmp; for(1..1000){ lock $x; print $fh "bla blalbla bla blal bla bla bla bla\n" ; #NOT SHARED!! +!! } print "$id>LOAD EXIT\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Threads: How to share a FileHandle to Write
by BrowserUk (Patriarch) on May 19, 2009 at 10:41 UTC | |
by gulden (Monk) on May 19, 2009 at 10:56 UTC | |
by BrowserUk (Patriarch) on May 19, 2009 at 11:22 UTC | |
by gulden (Monk) on May 19, 2009 at 14:45 UTC | |
by BrowserUk (Patriarch) on May 19, 2009 at 15:28 UTC | |
| |
|
Re: Threads: How to share a FileHandle to Write
by Anonymous Monk on May 19, 2009 at 09:58 UTC | |
|
Re: Threads: How to share a FileHandle to Write
by Anonymous Monk on May 19, 2009 at 10:03 UTC | |
|
Re: Threads: How to share a FileHandle to Write
by gulden (Monk) on May 19, 2009 at 10:48 UTC |