@@ -64,6 +64,49 @@ for $data-file.lines -> $line {
64
64
@data.push($line.split(','))
65
65
}
66
66
67
+ = head2 method lock
68
+
69
+ method lock(IO::Handle:D:
70
+ Bool:D :$non-blocking = False, Bool:D :$shared = False --> True
71
+ )
72
+
73
+ Places an advisory lock on the filehandle. If C < :$non-blocking > is C < True >
74
+ will L « C < fail > |/routine/fail» with C < X::IO::Lock > if lock could not be
75
+ obtained, otherwise will block until the lock can be placed. If C < :$shared >
76
+ is C < True > will place a shared (read) lock, otherwise will place an
77
+ exclusive (read) lock. On success, returns C < True > ; L « fails|/routine/fail »
78
+ with C < X::IO::Lock > if lock cannot be placed (e.g. when trying to place
79
+ a shared lock on a filehandle opened in write mode or trying to
80
+ place an exclusive lock on a filehandle opened in read mode).
81
+
82
+ You can use C < lock > again to replace an existing lock with another one.
83
+ Use L « C < close > |/routine/close» the filehandle or use
84
+ L « C < unlock > |/routine/unlock» to remove a lock.
85
+
86
+ = for code :skip-test
87
+ # One program writes, the other reads, and thanks to locks either
88
+ # will wait for the other to finish before proceeding to read/write
89
+
90
+ # Writer
91
+ with "foo".IO.open(:w) {
92
+ .lock;
93
+ .spurt: "I ♥ Perl 6!";
94
+ .close;
95
+ }
96
+
97
+ # Reader
98
+ with "foo".IO.open {
99
+ .lock: :shared;
100
+ .slurp.say; # OUTPUT: «I ♥ Perl 6!»
101
+ .close;
102
+ }
103
+
104
+ = head2 method unlock
105
+
106
+ method unlock(IO::Handle:D: --> True)
107
+
108
+ Removes a L « C < lock > |/routine/lock» from the filehandle.
109
+
67
110
= head2 method words
68
111
69
112
method words($count = Inf)
0 commit comments