local coroutine = coroutine
local table = table
local coroutine_create = coroutine.create
local coroutine_resume = coroutine.resume
local coroutine_yield = coroutine.yield
local tremove = table.remove
local coroutine_pool = setmetatable({}, { __mode = "kv" })
local function co_create(f)
local co = tremove(coroutine_pool)
if co == nil then
print('new co')
co = coroutine_create(function(...)
f(...)
while true do
-- recycle co into pool
f = nil
coroutine_pool[#coroutine_pool+1] = co
-- recv new main function f
f = coroutine_yield()
f(coroutine_yield())
end
end)
else
print('resume co')
coroutine_resume(co, f)
end
return co
end
local co = co_create(print)
coroutine_resume(co, 'hello world')
co = co_create(print)
coroutine_resume(co, 'hello world')
标签:协程,coroutine,create,resume,lua,co,local,pool From: https://www.cnblogs.com/kehuadong/p/16873687.html