So I've been messing around with coroutines, and found a rather strange problem. Apparently when I try to resume a yielded (suspended) coroutine, it doesn't seem to do anything. For this particular example, I tried to make two coroutines with a for loop, that both yield and resume eachother. Here's my code:
-- Create a new thread
local function thread(f)
local new=coroutine.create(f)
coroutine.resume(new)
return new
end
-- Make these global
local t1,t2
-- This will run first
t1=thread(function()
for i=1,10 do
print("FIRST",i)
coroutine.yield() -- Suspended
coroutine.resume(t2)
end
end)
-- This will run second, and at this point, "t1" should be suspended.
t2=thread(function()
for i=1,10 do
print("SECOND",i)
coroutine.resume(t1) -- So resume t1, and yield t2. But if you notice, the code after I yield in "t1" will resume this thread, which should now be suspended.
coroutine.yield()
end
end)
So here's the output:
FIRST 1
SECOND 1
But why does it stop at the first iteration? Shouldn't the constant loop of both of these coroutines resuming the other continue the for loop until one of them has terminated and dies?
If anyone can provide an explanation for this, I'd really appreciate it. Thanks.
