I'm trying to create a dispatcher which schedules multiple coroutines. The dispatcher needs to pause the coroutine, I can't figure out how to do this.
Update Instead of kill, I meant to pause the coroutine from the outside.
You can kill a coroutine by setting a debug hook on it that calls error() from that hook. The next time the hook is called, it will trigger error() call, which will abort the coroutine:
local co = coroutine.create(function()
while true do print(coroutine.yield()) end
end)
coroutine.resume(co, 1)
coroutine.resume(co, 2)
debug.sethook(co, function()error("almost dead")end, "l")
print(coroutine.resume(co, 3))
print(coroutine.status(co))
This prints:
2
3
false coro-kill.lua:6: almost dead
dead
library that will yield when you return true in the hook that been set with debug.sethook(co, function() return true end, "y")
the library is enough to create multitask lua system just run require("yieldhook") at very first of your code further info at the git
https://github.com/evg-zhabotinsky/yieldhook
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With