Created
September 10, 2013 14:57
-
-
Save kotaroito/6510661 to your computer and use it in GitHub Desktop.
LOCK_EX | LOCK_NB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use Fcntl qw(:flock LOCK_NB); | |
use Parallel::ForkManager; | |
my $pm = new Parallel::ForkManager(2); | |
for (1..2) { | |
my $pid = $pm->start and next; | |
write_file(); | |
$pm->finish; | |
} | |
$pm->wait_all_children; | |
sub write_file { | |
open (my $fh, '>', './touch') or die $!; | |
my $start = time; | |
my $locked = flock($fh, LOCK_EX|LOCK_NB); | |
sleep 5; | |
flock($fh, LOCK_UN); | |
my $end = time; | |
warn sprintf("proc(%d) - locked: %s, waited time: %d", $$, $locked ? 'true' : 'false', $end - $start); | |
close $fh; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment