Thread enviroments now allow you to use an await like feature when using threaded functions
This commit is contained in:
parent
26245032f7
commit
c251567804
@ -1481,7 +1481,7 @@ function thread:newFunction(func)
|
|||||||
local t = multi:newThread("TempThread",func,...)
|
local t = multi:newThread("TempThread",func,...)
|
||||||
t.OnDeath(function(self,status,...) rets = {...} end)
|
t.OnDeath(function(self,status,...) rets = {...} end)
|
||||||
t.OnError(function(self,e) err = e end)
|
t.OnError(function(self,e) err = e end)
|
||||||
return {
|
local temp = {
|
||||||
isTFunc = true,
|
isTFunc = true,
|
||||||
wait = wait,
|
wait = wait,
|
||||||
connect = function(f)
|
connect = function(f)
|
||||||
@ -1489,6 +1489,7 @@ function thread:newFunction(func)
|
|||||||
t.OnError(function(self,err) f(self, err) end)
|
t.OnError(function(self,err) f(self, err) end)
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
|
return temp,temp,temp,temp,temp,temp,temp
|
||||||
end
|
end
|
||||||
setmetatable(c,c)
|
setmetatable(c,c)
|
||||||
return c
|
return c
|
||||||
@ -1545,7 +1546,28 @@ function multi:newThread(name,func,...)
|
|||||||
if type(v)=="function" then
|
if type(v)=="function" then
|
||||||
rawset(t,k,thread:newFunction(v))
|
rawset(t,k,thread:newFunction(v))
|
||||||
else
|
else
|
||||||
Gref[k]=v
|
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
|
||||||
end
|
end
|
||||||
})
|
})
|
||||||
|
|||||||
16
test.lua
16
test.lua
@ -1,7 +1,19 @@
|
|||||||
package.path="?/init.lua;?.lua;"..package.path
|
package.path="?/init.lua;?.lua;"..package.path
|
||||||
multi,thread = require("multi"):init()
|
multi,thread = require("multi"):init()
|
||||||
multi:scheduleJob({min = 15, hour = 14},function()
|
multi:newThread(function()
|
||||||
print("hi!")
|
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)
|
end)
|
||||||
--min,hour,day,wday,month
|
--min,hour,day,wday,month
|
||||||
multi:mainloop()
|
multi:mainloop()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user