in reply to Re^2: Faster locking
in thread Faster locking

Could someone please confirm that Perl would skip trying to acquire a lock when the first 'if' condition is true?

Should I rather place the test in to a nested condition, completely outside of the first?

if ( (1 == 1) || (!flock $lockfile, LOCK_EX | LOCK_NB) )

PS: Things appear to be working as expected, but I would still like to know, for my own peace of mind.

Replies are listed 'Best First'.
Re^4: Faster locking
by bbs2web (Acolyte) on Apr 04, 2017 at 08:38 UTC

    Okay, so it works as expected:

    Test code:

    #!/usr/bin/perl use strict; use warnings; use Fcntl qw(:flock); open our $lockfile, '<', $0 || die $!; if ( (1 == 1) || (!flock $lockfile, LOCK_EX | LOCK_NB) ) { print "in loop\n"; sleep 30; if (flock $lockfile, LOCK_EX | LOCK_NB) { print "locked\n"; } else { print "not locked\n"; } sleep 30; }

    First instance:

    in loop locked

    Second instance:

    in loop not locked