Added thread.defer(func)

This commit is contained in:
Ryan Ward 2023-11-09 22:04:11 -05:00
parent 30db370cc9
commit 8d843a5958
3 changed files with 22 additions and 5 deletions

View File

@ -84,6 +84,7 @@ Allows the user to have multi auto set priorities (Requires chronos). Also adds
Added
---
- thread.defer(func) -- When using a co-routine thread or co-routine threaded function, defer will call it's function at the end of the the threads life through normal execution or an error. In the case of a function, when the function returns or errors.
- multi:setTaskDelay(delay), Tasks which are now tied to a processor can have an optional delay between the execution between each task. Useful perhaps for rate limiting. Without a delay all grouped tasks will be handled in one step.
- processor's now have a boost function which causes it to run its processes the number of times specified in the `boost(count)` function
- thread.hold will now use a custom hold method for objects with a `Hold` method. This is called like `obj:Hold(opt)`. The only argument passed is the optional options table that thread.hold can pass. There is an exception for connection objects. While they do contain a Hold method, the Hold method isn't used and is there for proxy objects, though they can be used in non proxy/thread situations. Hold returns all the arguments that the connection object was fired with.

View File

@ -1250,6 +1250,14 @@ function thread.request(t,cmd,...)
thread.requests[t.thread] = {cmd, multi.pack(...)}
end
function thread.defer(func)
local th = thread.getRunningThread()
local conn = (th.OnError + th.OnDeath)
conn(function()
func(th)
end)
end
function thread.getRunningThread()
local threads = globalThreads
local t = coroutine.running()

View File

@ -1,5 +1,5 @@
package.path = "../?/init.lua;../?.lua;"..package.path
multi, thread = require("multi"):init{print=true,warn=true,error=true,debugging=true}
multi, thread = require("multi"):init{print=true,warn=true,debugging=true}
-- require("multi.integration.priorityManager")
-- multi.debugging.OnObjectCreated(function(obj, process)
@ -101,14 +101,11 @@ multi, thread = require("multi"):init{print=true,warn=true,error=true,debugging=
multi:setTaskDelay(.05)
multi:newTask(function()
for i = 1, 100 do
for i = 1, 10 do
multi:newTask(function()
print("Task "..i)
end)
end
multi:newTask(function()
multi:Stop()
end)
end)
local conn = multi:newConnection()
@ -125,6 +122,17 @@ conn:Fire()
print(#conn)
thread:newThread("Test thread", function()
print("Starting thread!")
thread.defer(function() -- Runs when the thread finishes execution
print("Clean up time!")
end)
--[[
Do lot's of stuff
]]
thread.sleep(3)
end)
multi:mainloop()
-- local conn1, conn2, conn3 = multi:newConnection(nil,nil,true), multi:newConnection(), multi:newConnection()