calog/examples/scripts/libraries/taskSelfExit.lua

20 lines
912 B
Lua

-- Self-retiring task with taskExit().
-- A task that has finished its work can release its OWN context -- it need not wait for the
-- owner to taskClose() it. taskExit() is deferred: the rest of the current code runs, then the
-- context stops and the runtime reaps it. The owner just sees taskCount() drop.
-- run: bin/calog examples/scripts/libraries/taskSelfExit.lua
local worker = taskSpawn("lua", [[
calogPrint("worker: computing...")
local sum = 0
for i = 1, 5 do sum = sum + i end
calogPrint("worker: done (sum =", sum, "); retiring myself")
taskExit() -- retire my own context once this code finishes
]])
calogPrint("main: spawned a worker, taskCount =", taskCount())
while taskCount() > 0 do -- the worker self-exits; the runtime reaps it
timeSleep(20)
end
calogPrint("main: the worker retired itself, taskCount =", taskCount())
calogExit(0)