Rendezvous: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
|||
Line 143: | Line 143: | ||
Mother Goose out of ink! |
Mother Goose out of ink! |
||
</pre> |
</pre> |
||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
<lang AutoHotkey>OnMessage(0x4a, "PrintMonitor") |
<lang AutoHotkey>OnMessage(0x4a, "PrintMonitor") |
||
Line 221: | Line 222: | ||
Return -1 |
Return -1 |
||
}</lang> |
}</lang> |
||
=={{header|C}}== |
=={{header|C}}== |
||
Basically just synched threads doing printing: since task didn't ask for service type or resource enumeration, and "message passing is stupid" (c.f. talk), the guarding thread is no more than a glorified mutex, hence completely cut out, leaving the threads directly check ink and do print. |
Basically just synched threads doing printing: since task didn't ask for service type or resource enumeration, and "message passing is stupid" (c.f. talk), the guarding thread is no more than a glorified mutex, hence completely cut out, leaving the threads directly check ink and do print. |
||
Line 1,005: | Line 1,007: | ||
Like in the rendezvous mechanism, parameters are not marshalled. This is because sharing immutable data between threads is safe. |
Like in the rendezvous mechanism, parameters are not marshalled. This is because sharing immutable data between threads is safe. |
||
In contrast to ADA, the parameters are buffered until the printer becomes ready. But with a synchronous communication mechanism, this should not cause problems. |
In contrast to ADA, the parameters are buffered until the printer becomes ready. But with a synchronous communication mechanism, this should not cause problems. |
||
=={{header|Perl 6}}== |
|||
Perl 6 has no built-in support for rendezvous. I tried to simulate it using message passing and a lock - not sure if that counts. |
|||
{{works with|Rakudo|2016.08}} |
|||
<lang perl6>class X::OutOfInk is Exception { |
|||
method message() { "Printer out of ink" } |
|||
} |
|||
class Printer { |
|||
has Str $.id; |
|||
has Int $.ink = 5; |
|||
has Lock $!lock .= new; |
|||
has ::?CLASS $.fallback; |
|||
method print ($line) { |
|||
$!lock.protect: { |
|||
if $!ink { say "$!id: $line"; $!ink-- } |
|||
elsif $!fallback { $!fallback.print: $line } |
|||
else { die X::OutOfInk.new } |
|||
} |
|||
} |
|||
} |
|||
my $printer = |
|||
Printer.new: id => 'main', fallback => |
|||
Printer.new: id => 'reserve'; |
|||
sub client ($id, @lines) { |
|||
start { |
|||
for @lines { |
|||
$printer.print: $_; |
|||
CATCH { |
|||
when X::OutOfInk { note "<$id stops for lack of ink>"; exit } |
|||
} |
|||
} |
|||
note "<$id is done>"; |
|||
} |
|||
} |
|||
await |
|||
client('Humpty', q:to/END/.lines), |
|||
Humpty Dumpty sat on a wall. |
|||
Humpty Dumpty had a great fall. |
|||
All the king's horses and all the king's men, |
|||
Couldn't put Humpty together again. |
|||
END |
|||
client('Goose', q:to/END/.lines); |
|||
Old Mother Goose, |
|||
When she wanted to wander, |
|||
Would ride through the air, |
|||
On a very fine gander. |
|||
Jack's mother came in, |
|||
And caught the goose soon, |
|||
And mounting its back, |
|||
Flew up to the moon. |
|||
END</lang> |
|||
{{out}} |
|||
<pre> |
|||
main: Humpty Dumpty sat on a wall. |
|||
main: Old Mother Goose, |
|||
main: Humpty Dumpty had a great fall. |
|||
main: When she wanted to wander, |
|||
main: All the king's horses and all the king's men, |
|||
reserve: Would ride through the air, |
|||
reserve: Couldn't put Humpty together again. |
|||
reserve: On a very fine gander. |
|||
<Humpty is done> |
|||
reserve: Jack's mother came in, |
|||
reserve: And caught the goose soon, |
|||
<Goose stops for lack of ink> |
|||
</pre> |
|||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
Line 1,304: | Line 1,232: | ||
uncaught exception: 'out-of-ink |
uncaught exception: 'out-of-ink |
||
</lang> |
</lang> |
||
=={{header|Raku}}== |
|||
(formerly Perl 6) |
|||
Perl 6 has no built-in support for rendezvous. I tried to simulate it using message passing and a lock - not sure if that counts. |
|||
{{works with|Rakudo|2016.08}} |
|||
<lang perl6>class X::OutOfInk is Exception { |
|||
method message() { "Printer out of ink" } |
|||
} |
|||
class Printer { |
|||
has Str $.id; |
|||
has Int $.ink = 5; |
|||
has Lock $!lock .= new; |
|||
has ::?CLASS $.fallback; |
|||
method print ($line) { |
|||
$!lock.protect: { |
|||
if $!ink { say "$!id: $line"; $!ink-- } |
|||
elsif $!fallback { $!fallback.print: $line } |
|||
else { die X::OutOfInk.new } |
|||
} |
|||
} |
|||
} |
|||
my $printer = |
|||
Printer.new: id => 'main', fallback => |
|||
Printer.new: id => 'reserve'; |
|||
sub client ($id, @lines) { |
|||
start { |
|||
for @lines { |
|||
$printer.print: $_; |
|||
CATCH { |
|||
when X::OutOfInk { note "<$id stops for lack of ink>"; exit } |
|||
} |
|||
} |
|||
note "<$id is done>"; |
|||
} |
|||
} |
|||
await |
|||
client('Humpty', q:to/END/.lines), |
|||
Humpty Dumpty sat on a wall. |
|||
Humpty Dumpty had a great fall. |
|||
All the king's horses and all the king's men, |
|||
Couldn't put Humpty together again. |
|||
END |
|||
client('Goose', q:to/END/.lines); |
|||
Old Mother Goose, |
|||
When she wanted to wander, |
|||
Would ride through the air, |
|||
On a very fine gander. |
|||
Jack's mother came in, |
|||
And caught the goose soon, |
|||
And mounting its back, |
|||
Flew up to the moon. |
|||
END</lang> |
|||
{{out}} |
|||
<pre> |
|||
main: Humpty Dumpty sat on a wall. |
|||
main: Old Mother Goose, |
|||
main: Humpty Dumpty had a great fall. |
|||
main: When she wanted to wander, |
|||
main: All the king's horses and all the king's men, |
|||
reserve: Would ride through the air, |
|||
reserve: Couldn't put Humpty together again. |
|||
reserve: On a very fine gander. |
|||
<Humpty is done> |
|||
reserve: Jack's mother came in, |
|||
reserve: And caught the goose soon, |
|||
<Goose stops for lack of ink> |
|||
</pre> |
|||
=={{header|Tcl}}== |
=={{header|Tcl}}== |