• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Multi-threads lib

Mock

Mock the bear (MTB)
Joined
Jul 29, 2008
Messages
619
Reaction score
106
Location
Brazil
An simple multi thereads lib ;D
you can run alot functions at same time ^^
Add it to global.lua (to otserv 0.3.6) or functions.lua (tfs)
Lua:
-- Lib by mock!
thread = {f_=function(...) end}
function thread:create_interface() -- function by mock
  local object = {F_s={}}
  setmetatable(object, {__index=thread})
  return object
end


function thread:add(func,priority,...) -- function by mock
  table.insert(self.F_s,1,{coroutine.create(func or function(...) end),priority or 1,{...}})
end

function thread:run(interval) -- function by mock
	function disp(self,interval)
			for id,var in pairs(self.F_s) do
				for prit=1,var[2] do
					if type(var[1]) == 'thread' and coroutine.status(var[1]) ~= 'dead' then
						local stat, ret = coroutine.resume(var[1],unpack(var[3] or {}))
						if stat == false then
							print(ret)
							self.F_s[id] = nil
						end
					else
						self.F_s[id] = nil
					end
				end
			end
		if #self.F_s > 0 then
			if (interval or 0 ) == 0 then
				disp(self,interval)
			else
				addEvent(disp,interval*100,self,interval)
			end
		end
	end
	disp(self,interval)
end

I will sho an wxample to use.
Lua:
local threads = thread:vreate_interface()
threads:add(function()
for i=1,10 do
   print('i='..i)
   coroutine.yield() -- Here pause function and start second function
end
end,2)
threads:add(function()
    local file = io.open('bears.txt','r')
	print('Opening')
    coroutine.yield() -- pause
    if file then
	   print('Reading')
       print(file:read(-1))
	   coroutine.yield() -- paause
       file:close()
	   print('Closing')
       return true -- end
    else
	    print('Creating')
		file = io.open('ursos.txt','w')
		coroutine.yield() -- pause
		print('Escrevendo')
		file:write('omg!')
		coroutine.yield() -- pause
		print('Bye')
		file:close()
		return
	end
end,1)
function show_me(...) print('show:',...) end
threads:add(show_me,1,'q','aa')
-- And start
threads:run()
And it print:
Code:
show:	q	aa
Opening
i=1
Creating
i=2
Escrevendo
i=3
Bye
i=4
i=5
So.. you start an function and put coroutine.yield() to pause function.
while your function is paused all other function will start and only will stop when it go to pause or finish.
Function 1 start, pause > go to function 2 start, pause > go to function 1....
;D
You can run 2 loops at same time:
Lua:
local threads = thread:create_interface()
threads:add(function()
n = 0
while true do
   n = n+1
   print('Hail',n)
   coroutine.yield()
end
end,1)
threads:add(function()
n = 0
while true do
   n = n+1
   print('XD',n)
   coroutine.yield()
end
end,1)
threads:run()
Code:
XD	1
Hail	1
XD	2
Hail	3
XD	4
Hail	5
XD	6
Hail	7
XD	8
Hail	9
XD	10
Hail	11
...
Here are function syntax.
thread:create_interface() Returns an object to use in add() and run()
obj:add(function[,int priority]) Here you put function in argument 1# and 2# to time to function execute after a pause.
threads:run([int interval]) Here you start all thereads, use inserval only in otserver for your server dont freeze (interval = n*100)
 
Dangerous for the noobs here? Ha! This is more like 'You just left to go pick up a pizza with the children at home playing near an unlocked rifle cabinet.'

Excellent work Mock.
 
Back
Top