用于在Lua中创建协程/线程的函数。

是否有可能获取用于创建协同程序的原始函数?

thread = coroutine.create(function()
   -- 代码
end)

f = get_function_from_thread(thread)
点赞
用户107090
用户107090

你不能直接这样做,但你可以重新定义 coroutine.create :

local create=coroutine.create
local created={}

function coroutine.create(f)
   local t=create(f)
   created[t]=f
   return t
end

function get_function_from_thread(t)
   return created[t]
end

如果你要创建大量协程,请考虑将 created 设置为一个弱表。

2015-03-03 11:31:34