Demonstrate the “rendezvous” communications technique by implementing a printer monitor.

without js -- (threads)
constant print_cs = init_cs()
enum NAME,INK
sequence printers = {{"main",5},
                     {"reserve",5}}
 
procedure printer(string name, sequence s)
    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 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 = {"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(printer,{"hd",hd}),
                     create_thread(printer,{"mg",mg})}
wait_thread(hThreads)
