Rendezvous: Difference between revisions

Added Wren
(implement in nim lang)
(Added Wren)
Line 1,870:
after 1000
thread::broadcast thread::exit</lang>
 
=={{header|Wren}}==
This uses fibers, which are always synchronous in Wren, to simulate the rendezvous mechanism.
<lang ecmascript>class Printer {
construct new(id) {
_id = id
_ink = 5
}
ink { _ink }
ink=(v) { _ink = v }
 
print(text) {
System.write("%(_id): ")
for (c in text) System.write(c)
System.print()
_ink = _ink - 1
}
}
 
var ptrMain = Printer.new("Main ")
var ptrReserve = Printer.new("Reserve")
 
var 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."
]
 
var 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."
]
 
var task = Fn.new { |name|
var lines = (name == "Humpty Dumpty") ? hd : mg
for (line in lines) {
if (ptrMain.ink > 0) {
ptrMain.print(line)
Fiber.yield()
} else if (ptrReserve.ink > 0) {
ptrReserve.print(line)
Fiber.yield()
} else {
Fiber.abort("ERROR : Reserve printer ran out of ink in %(name) task.")
}
}
}
 
var rhymes = ["Humpty Dumpty", "Mother Goose"]
var tasks = List.filled(2, null)
for (i in 0..1) {
tasks[i] = Fiber.new(task)
tasks[i].call(rhymes[i])
}
 
while (true) {
for (i in 0..1) {
if (!tasks[i].isDone) {
var error = tasks[i].try()
if (error) {
System.print(error)
return
}
}
}
}</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.
Reserve: Jack's mother came in,
Reserve: And caught the goose soon,
ERROR : Reserve printer ran out of ink in Mother Goose task.
</pre>
 
=={{header|zkl}}==
9,476

edits