diff --git a/changes.md b/changes.md index 8f4c714..c93fe5e 100644 --- a/changes.md +++ b/changes.md @@ -3,6 +3,34 @@ Update 14.1.0 Bug Fixes and a change ------------- # Added: +- thread enviroments are able to interact with threaded functions and wait when there is the presence of variables. Only works when creating "Globals" inside of a thread. The way the enviroment has been set up is that it sets your "Global" as a "GLocal" a global variable local to the threaded enviroment. This does not have the access speed benifits that using pure locals have.. +```lua +package.path="?/init.lua;?.lua;"..package.path +multi,thread = require("multi"):init() +a,b = 6,7 +multi:newThread(function() + function test() -- Auto converts your function into a threaded function + thread.sleep(1) + return 1,2 + end + a,b = test().wait() -- Will modify Global + -- when wait is used the special metamethod routine is not triggered and variables are set as normal + a,b = test() -- Will modify GLocal + -- the threaded function test triggers a special routine within the metamethod that alters the thread's enviroment instead of the global enviroment. + print("Waited:",a,b) + --This returns instantly even though the function isn't done! + test().connect(function(a,b) + print("Connected:",a,b) + os.exit() + end) + -- This waits for the returns since we are demanding them +end) +multi:newAlarm(2):OnRing(function() + print(a,b) +end) +--min,hour,day,wday,month +multi:mainloop() +``` - multi:scheduleJob(time,func) - time.min -- Minute a value of (0-59) - time.hour -- Hour a value of (0-23) diff --git a/multi/init.lua b/multi/init.lua index da7a70f..3c0fd3c 100644 --- a/multi/init.lua +++ b/multi/init.lua @@ -1559,10 +1559,10 @@ function multi:newThread(name,func,...) table.insert(s,e) table.insert(s,f) local x = table.remove(_G["_stack_"]) - Gref[k]=x + rawset(t,k,x) else local x = table.remove(_G["_stack_"]) - Gref[k]=x + rawset(t,k,x) end else Gref[k]=v diff --git a/test.lua b/test.lua index bbff7c8..d391b82 100644 --- a/test.lua +++ b/test.lua @@ -1,18 +1,22 @@ package.path="?/init.lua;?.lua;"..package.path multi,thread = require("multi"):init() +a,b = 6,7 multi:newThread(function() function test() thread.sleep(1) return 1,2 end + a,b = test().wait() + print("Waited:",a,b) --This returns instantly even though the function isn't done! test().connect(function(a,b) print("Connected:",a,b) + os.exit() end) -- This waits for the returns since we are demanding them - a,b = test() - print("Waited:",a,b) - os.exit() +end) +multi:newAlarm(2):OnRing(function() + print(a,b) end) --min,hour,day,wday,month multi:mainloop()