As we saw in c, it always specifies what is safe with thread, what is unsafe. For example, strtok is unsafe with thread.
Perl does not do this, and this is extreamly dangerous. Of course, it is not a surprise to me, as Perl only started to have this real thread stuff since 5.8. However Perl should start this ASAP, otherwise it will bite everyone.
For example, I found Tie::File is not safe with thread. Try the following code on win32, see how account.txt is messed up.
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;
}
}
Foot Notes:
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.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
|
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.