Created
January 24, 2019 08:11
-
-
Save sharonovd/0abca667c3b8641d9807320d244e3c8f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local fiber = require('fiber') | |
function another_func() | |
fiber.sleep(1) | |
fiber.yield() | |
end | |
function test() | |
local fib = fiber.create(another_func) | |
fib:set_joinable(true) | |
fib:join() | |
end | |
tasks = {} | |
function start(func, name) | |
local worker = fiber.create(function() | |
while true do | |
local ok, _ = pcall(func) | |
fiber.testcancel() | |
fiber.sleep(0.1) | |
end | |
end) | |
tasks[name] = worker | |
end | |
function stop(name) | |
tasks[name]:cancel() | |
end | |
start(test, 'test') | |
stop('test') | |
while tasks[name] and tasks[name]:status() ~= 'dead' do | |
print 'Not ok' | |
print(tasks[name]:status()) | |
fiber.sleep(1) | |
end | |
print 'Success!' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This leads to: