Wednesday, May 11, 2011

threads lock example

threads lock is used for web page manipulation. Suppose one person see one web page, at the same time another person edition or updating. In this case you can use variable lock.


use threads;
use threads::shared;

my $sem :shared;
sub tprint {
    my $tid = threads->tid;
    lock $sem;
    print "$tid: ",@_, "\n";
}

sub twarn {
    my $tid = threads->tid;
    lock $sem;
    warn "$tid: ", @_;
}

No comments: