V15.0.0 #19
@ -1179,7 +1179,9 @@ function multi:newISOThread(name,func,...)
|
||||
if type(name) == "function" then
|
||||
name = "Thread#"..threadCount
|
||||
end
|
||||
local Gref = {}
|
||||
local env = {
|
||||
THREAD_NAME = name,
|
||||
thread = thread,
|
||||
multi = multi,
|
||||
coroutine = coroutine,
|
||||
@ -1197,7 +1199,16 @@ function multi:newISOThread(name,func,...)
|
||||
env[i]=v
|
||||
end
|
||||
end
|
||||
local c={}
|
||||
function c:inject(tab)
|
||||
for i,v in pairs(tab) do
|
||||
Gref[i] = v
|
||||
env[i] = v
|
||||
end
|
||||
end
|
||||
function c:start()
|
||||
setmetatable(env,{
|
||||
__index = Gref,
|
||||
__newindex = function(t,k,v)
|
||||
if type(v)=="function" then
|
||||
rawset(t,k,thread:newFunction(v))
|
||||
@ -1206,14 +1217,13 @@ function multi:newISOThread(name,func,...)
|
||||
end
|
||||
end
|
||||
})
|
||||
local func = multi.setEnv(func,env)
|
||||
local c={}
|
||||
self.thread=coroutine.create(multi.setEnv(func,env))
|
||||
end
|
||||
env.self = c
|
||||
c.TempRets = {nil,nil,nil,nil,nil,nil,nil,nil,nil,nil}
|
||||
c.startArgs = {...}
|
||||
c.ref={}
|
||||
c.Name=name
|
||||
c.thread=coroutine.create(func)
|
||||
c.sleep=1
|
||||
c.Type="thread"
|
||||
c.TID = threadid
|
||||
|
||||
@ -43,7 +43,17 @@ function multi:getPlatform()
|
||||
end
|
||||
|
||||
THREAD.newFunction=thread.newFunction
|
||||
multi.newSystemThread = multi.newISOThread
|
||||
local id = 0
|
||||
function multi:newSystemThread(name,func,...)
|
||||
local t = multi:newISOThread(name,func,...)
|
||||
t:inject{
|
||||
GLOBAL = GLOBAL,
|
||||
THREAD = THREAD,
|
||||
THREAD_ID = id
|
||||
}
|
||||
id = id + 1
|
||||
t:start()
|
||||
end
|
||||
-- System threads as implemented here cannot share memory, but use a message passing system.
|
||||
-- An isolated thread allows us to mimic that behavior so if access data from the "main" thread happens things will not work. This behavior is in line with how the system threading works
|
||||
|
||||
|
||||
@ -82,7 +82,6 @@ local function INIT()
|
||||
function THREAD.getID()
|
||||
return THREAD_ID
|
||||
end
|
||||
_G.THREAD_ID = 0
|
||||
function THREAD.sleep(n)
|
||||
thread.sleep(n)
|
||||
end
|
||||
|
||||
22
test.lua
22
test.lua
@ -3,16 +3,20 @@ multi,thread = require("multi"):init()
|
||||
GLOBAL,THREAD = require("multi.integration.pesudoManager"):init()
|
||||
test = true
|
||||
local haha = true
|
||||
jq = multi:newSystemThreadedJobQueue(100) -- Job queue with 4 worker threads
|
||||
func = jq:newFunction("test",function(a,b)
|
||||
THREAD.sleep(2)
|
||||
return a+b
|
||||
local test = multi:newSystemThreadedTable("test"):init()
|
||||
test['hi'] = "Hello World!!!"
|
||||
test['bye'] = "Bye World!!!"
|
||||
multi:newSystemThread("test_1",function()
|
||||
print(THREAD_NAME,THREAD_ID,THREAD.getName())
|
||||
print("thread",GLOBAL,THREAD,test,haha)
|
||||
tab = THREAD.waitFor("test"):init()
|
||||
print(tab["hi"])
|
||||
end)
|
||||
|
||||
for i = 1,100 do
|
||||
func(i,i*3).connect(function(data)
|
||||
print(data)
|
||||
multi:newSystemThread("test_2",function()
|
||||
print(THREAD_NAME,THREAD_ID,THREAD.getName())
|
||||
print("thread",GLOBAL,THREAD,test,haha)
|
||||
tab = THREAD.waitFor("test"):init()
|
||||
print(tab["bye"])
|
||||
end)
|
||||
end
|
||||
|
||||
multi:mainloop()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user