Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
document Lock
- Loading branch information
Showing
1 changed file
with
59 additions
and
0 deletions.
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| =begin pod | ||
| =TITLE class Lock | ||
| =SUBTITLE Low-level thread locking primitive | ||
| class Lock { ... } | ||
| A Lock is a low-level constructor for ensuring that only one thread works with | ||
| a certain object at a given time, or runs a piece of code (called the | ||
| I<critical section>). | ||
| my $x = 0; | ||
| my $l = Lock.new; | ||
| await for (^10).map: { | ||
| $l.protect({ $x++ }); | ||
| } | ||
| say $x; # | ||
| High-level Perl 6 code should awoid the direct usage of locks, because they | ||
| are not composable. Instead, high-level constructs such as | ||
| L<Promise|/type/Promise>, L<Channel|/type/Channel> and Supply should be used | ||
| whenever possible. | ||
| =head1 Methods | ||
| =head2 method protect | ||
| method protect(Lock:D: &code) | ||
| Runs C<&code> and makes sure it is only run in one thread at once. | ||
| =head2 method lock | ||
| method lock(Lock:D:) | ||
| Acquires the lock. If it is currently not available, waits for it. | ||
| =head2 method unlock | ||
| method lock(Lock:D:) | ||
| Releases the lock. | ||
| =head2 method condition | ||
| my class ConditionVariable { | ||
| method wait(); | ||
| method signal(); | ||
| method signal_all(); | ||
| } | ||
| method condition(Lock:D:) returns ConditionVariable:D | ||
| Returns a condition variable. Compare | ||
| L<http://web.stanford.edu/class/cs140/cgi-bin/lecture.php?topic=locks> or | ||
| L<http://en.wikipedia.org/wiki/Monitor_%28synchronization%29> for background. | ||
| =end pod |