Rendezvous: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (Remove bogus link. Minor vandalism, added anonymously in Nov 2009) |
|||
Line 987: | Line 987: | ||
reserve: And caught the goose soon, |
reserve: And caught the goose soon, |
||
<Goose stops for lack of ink> |
<Goose stops for lack of ink> |
||
</pre> |
|||
=={{header|Phix}}== |
|||
Phix has no rendezvous mechanism, the following achieves something similar using a simple mutex. |
|||
<lang Phix>constant print_cs = init_cs() |
|||
enum NAME,INK |
|||
sequence printers = {{"main",5}, |
|||
{"reserve",5}} |
|||
procedure printer(sequence args) |
|||
{string name, sequence s} = args |
|||
try |
|||
for i=1 to length(s) do |
|||
enter_cs(print_cs) |
|||
for p=1 to length(printers) do |
|||
if printers[p][INK]!=0 then |
|||
printers[p][INK] -= 1 |
|||
printf(1,"%s/%s: %s\n",{name,printers[p][NAME],s[i]}) |
|||
exit |
|||
elsif p=length(printers) then |
|||
throw("out of ink") |
|||
end if |
|||
end for |
|||
leave_cs(print_cs) |
|||
end for |
|||
exit_thread(0) |
|||
catch e |
|||
printf(1,"exception(%s): %s\n",{name,e[E_USER]}) |
|||
leave_cs(print_cs) |
|||
exit_thread(1) |
|||
end try |
|||
end procedure |
|||
constant r_printer = routine_id("printer") |
|||
constant hd = {"hd",{"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."}}, |
|||
mg = {"mg",{"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."}} |
|||
sequence hThreads = {create_thread(r_printer,{hd}), |
|||
create_thread(r_printer,{mg})} |
|||
wait_thread(hThreads)</lang> |
|||
{{out}} |
|||
<pre> |
|||
hd/main: Humpty Dumpty sat on a wall. |
|||
mg/main: Old Mother Goose |
|||
mg/main: When she wanted to wander, |
|||
hd/main: Humpty Dumpty had a great fall. |
|||
hd/main: All the king's horses and all the king's men |
|||
hd/reserve: Couldn't put Humpty together again. |
|||
mg/reserve: Would ride through the air |
|||
mg/reserve: On a very fine gander. |
|||
mg/reserve: Jack's mother came in, |
|||
mg/reserve: And caught the goose soon, |
|||
exception(mg): out of ink |
|||
</pre> |
</pre> |
||