Rendezvous: Difference between revisions

Content added Content deleted
(Go solution)
(→‎{{header|Go}}: updated to use new WaitGroup feature. also improved exception message.)
Line 230: Line 230:
"fmt"
"fmt"
"strings"
"strings"
"sync"
)
)


Line 249: Line 250:
reservePrinter := startMonitor(newPrinter(5), nil)
reservePrinter := startMonitor(newPrinter(5), nil)
mainPrinter := startMonitor(newPrinter(5), reservePrinter)
mainPrinter := startMonitor(newPrinter(5), reservePrinter)
var busy sync.WaitGroup

hdDone := make(chan int)
busy.Add(2)
go writer(mainPrinter, "hd", hdText, &busy)
mgDone := make(chan int)
go writer(mainPrinter, hdText, hdDone)
go writer(mainPrinter, "mg", mgText, &busy)
busy.Wait()
go writer(mainPrinter, mgText, mgDone)
<-hdDone
<-mgDone
}
}


Line 303: Line 302:
}
}


func writer(printer *rSync, text string, done chan int) {
func writer(printer *rSync, id, text string, busy *sync.WaitGroup) {
for _, line := range strings.Split(text, "\n", -1) {
for _, line := range strings.Split(text, "\n", -1) {
if e := printer.rendezvous(line); e != nil {
if e := printer.rendezvous(line); e != nil {
fmt.Println("**** writer received exception:", e, "****")
fmt.Println("**** print job", id, "terminated:", e, "****")
break
break
}
}
}
}
done <- 0
busy.Done()
}</lang>
}</lang>
Output:
Output:
<pre>Old Mother Goose,
<pre>
Old Mother Goose,
Humpty Dumpty sat on a wall.
Humpty Dumpty sat on a wall.
Humpty Dumpty had a great fall.
Humpty Dumpty had a great fall.
Line 323: Line 323:
Jack's mother came in,
Jack's mother came in,
And caught the goose soon,
And caught the goose soon,
**** writer received exception: out of ink ****</pre>
**** print job mg terminated: out of ink ****
</pre>


=={{header|Oz}}==
=={{header|Oz}}==