Discuss, ask for help, share ideas, give suggestions, read tutorials, and tell us about bugs you have found with MicroMacro in here.
Do not post RoM-Bot stuff here. There is a subforum for that.
Forum rules
This is a sub-forum for things specific to MicroMacro.
This is not the place to ask questions about the RoM bot, which uses MicroMacro. There is a difference.
-
zer0
- Posts: 213
- Joined: Sat Feb 16, 2008 11:55 pm
#1
Post
by zer0 » 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:
Code: Select all
function foo()
for i=1, 10 do
print("i=", i)
end
end
foo()
print("in-between")
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:
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")
But the "in-between" showed at the end, which suggests it's still single threaded, am I wrong?
-
Administrator
- Site Admin
- Posts: 5307
- Joined: Sat Jan 05, 2008 4:21 pm
#2
Post
by Administrator » Thu Jan 15, 2009 4:13 am
Both parts need to be coroutines. That is, the print statement needs to be in a separate coroutine. Then you would need to resume other threads within each other. Try maybe something like this:
Code: Select all
local threads = {};
function createThread(func)
local tmp = coroutine.create(func);
table.insert(threads, tmp);
end
function main()
local func1 = function()
while(true) do
for i = 1,10 do
print(i);
coroutine.yield();
end
end
end
local func2 = function ()
while(true) do
print("Hello World");
coroutine.yield();
end
end
createThread(func1);
createThread(func2);
while(true) do
for i,v in pairs(threads) do
coroutine.resume(v);
end
end
end
startMacro(main);
Also remember that once the function returns that thread is "dead". That's why I wrapped both functions in while loops for this example.
-
zer0
- Posts: 213
- Joined: Sat Feb 16, 2008 11:55 pm
#3
Post
by zer0 » Thu Jan 15, 2009 6:56 am
Oh bugger looks like it's more complicated than I thought.
Give me awhile to digest all this.
Thanks as always Elv.
Who is online
Users browsing this forum: No registered users and 4 guests