diff --git a/docs/changes.md b/docs/changes.md index b09d3d0..294a38c 100644 --- a/docs/changes.md +++ b/docs/changes.md @@ -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. diff --git a/init.lua b/init.lua index 9e70cfd..f758de4 100644 --- a/init.lua +++ b/init.lua @@ -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() diff --git a/tests/test.lua b/tests/test.lua index 62c793f..d9cd152 100644 --- a/tests/test.lua +++ b/tests/test.lua @@ -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()