Thread enviroments now allow you to use an await like feature when using threaded functions

This commit is contained in:
Ryan Ward 2020-01-28 23:35:23 -05:00
parent 26245032f7
commit c251567804
2 changed files with 38 additions and 4 deletions

View File

@ -1481,7 +1481,7 @@ function thread:newFunction(func)
local t = multi:newThread("TempThread",func,...)
t.OnDeath(function(self,status,...) rets = {...} end)
t.OnError(function(self,e) err = e end)
return {
local temp = {
isTFunc = true,
wait = wait,
connect = function(f)
@ -1489,6 +1489,7 @@ function thread:newFunction(func)
t.OnError(function(self,err) f(self, err) end)
end
}
return temp,temp,temp,temp,temp,temp,temp
end
setmetatable(c,c)
return c
@ -1544,9 +1545,30 @@ function multi:newThread(name,func,...)
__newindex = function(t,k,v)
if type(v)=="function" then
rawset(t,k,thread:newFunction(v))
else
if type(v)=="table" then
if v.isTFunc then
if not _G["_stack_"] or #_G["_stack_"]==0 then
_G["_stack_"] = {}
local s = _G["_stack_"]
local a,b,c,d,e,f,g = v.wait()
table.insert(s,a)
table.insert(s,b)
table.insert(s,c)
table.insert(s,d)
table.insert(s,e)
table.insert(s,f)
Gref[k]=table.remove(_G["_stack_"])
else
Gref[k]=table.remove(_G["_stack_"])
end
else
Gref[k]=v
end
else
Gref[k]=v
end
end
end
})
setfenv(func,env)

View File

@ -1,7 +1,19 @@
package.path="?/init.lua;?.lua;"..package.path
multi,thread = require("multi"):init()
multi:scheduleJob({min = 15, hour = 14},function()
print("hi!")
multi:newThread(function()
function test()
thread.sleep(1)
return 1,2
end
-- This returns instantly even though the function isn't done!
test().connect(function(...)
print(...)
end)
print("Done")
-- This waits for the returns since we are demanding them
a,b = test()
print(a,b)
os.exit()
end)
--min,hour,day,wday,month
multi:mainloop()