Documentation for routine lock

Documentation for routine lock, assembled from the following types:

class IO::Handle

From IO::Handle

(IO::Handle) method lock

method lock(IO::Handle:D: Bool:D :$non-blocking = FalseBool:D :$shared = False --> True)

Places an advisory lock on the filehandle. If :$non-blocking is True will fail with X::IO::Lock if lock could not be obtained, otherwise will block until the lock can be placed. If :$shared is True will place a shared (read) lock, otherwise will place an exclusive (read) lock. On success, returns True; fails with X::IO::Lock if lock cannot be placed (e.g. when trying to place a shared lock on a filehandle opened in write mode or trying to place an exclusive lock on a filehandle opened in read mode).

You can use lock again to replace an existing lock with another one. Use close the filehandle or use unlock to remove a lock.

# One program writes, the other reads, and thanks to locks either 
# will wait for the other to finish before proceeding to read/write 

# Writer with "foo".IO.open(:w) { .lock; .spurt: "I ♥ Perl 6!"; .close; }

# Reader with "foo".IO.open { .lock: :shared; .slurp.say; # OUTPUT: «I ♥ Perl 6!␤» .close; }

class Lock

From Lock

(Lock) method lock

Defined as:

method lock(Lock:D:)

Acquires the lock. If it is currently not available, waits for it.

my $l = Lock.new;
$l.lock;