Code (only run function is relevant):
Spoiler
local threads = {}
Thread = {}
Thread.resume = function(self)
self.running = true
end
Thread.pause = function(self)
self.running = false
end
function createThread(func)
local thr = {}
thr.__index = Thread
thr.func = func
thr.running = false
thr.waitingFor = nil
local cor = coroutine.create(func)
thr.cor = cor
setmetatable(thr, thr)
threads[#threads + 1] = thr
return thr
end
local function countAlive()
local alive = 0
for i = 1, #threads do
local thread = threads[i]
if thread ~= nil then
if coroutine.status(thread.cor) ~= "dead" and thread.running == true then
alive = alive + 1
end
end
end
return alive
end
function run()
local eventData = {}
while countAlive() > 0 do
for i = 1, #threads do
local thread = threads[i]
if thread ~= nil then
if (thread.waitingFor == nil or thread.waitingFor == eventData[1]) and coroutine.status(thread.cor) ~= "dead" and thread.running == true then
local success, message = coroutine.resume(thread.cor, unpack(eventData))
if success == false then
error(message)
end
thread.waitingFor = message
end
end
end
eventData = { os.pullEventRaw() }
end
end
As far as I can tell, I'm doing this in the same way as the parallel API, but I can't work out what I'm doing wrong.
(Forum indentation is (still readable, but) kinda derpy, sorry!)
EDIT: I'm currently doing a os.startTimer(0.5) before pulling an event as a workaround, but I'm wondering if there's a better way (parallel doesn't seem to do this)