Lua - Creating multi-threaded scripts.
Posted: Thu Jan 15, 2009 12:47 am
Hey all,
Wondering how to do multi-threaded scripts, I'm pretty sure it involves the use of co-routines but I don't entirely understand the documentation.
http://lua-users.org/wiki/CoroutinesTutorial
So just to give you a quick example, if I had the following:
I want the output to be somewhere along the lines of:
note - the "in-between" should be anywhere since my requirements are that it continues executing and does the foo function is a separate thread.
How would I do the above code to make it multi-threaded?
I tried doing this code:
But the "in-between" showed at the end, which suggests it's still single threaded, am I wrong?
Wondering how to do multi-threaded scripts, I'm pretty sure it involves the use of co-routines but I don't entirely understand the documentation.
http://lua-users.org/wiki/CoroutinesTutorial
So just to give you a quick example, if I had the following:
Code: Select all
function foo()
for i=1, 10 do
print("i=", i)
end
end
foo()
print("in-between")
Code: Select all
i=1
in-between
i=2
...
i=10
How would I do the above code to make it multi-threaded?
I tried doing this code:
Code: Select all
co = coroutine.create(function ()
for i=1,10 do
print("i", i)
--coroutine.yield()
end
end)
coroutine.resume(co)
print("in-between")