lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi Dominique,
        It took me some time but I finally got the coroutines working. I have these 2 scripts running in parallel and they are executing fine:

script1:
function D()
    while true do
        for i=1,3 do
            print('D')
        end
        print(coroutine.yield(4,5,6))
    end
end

function co()
    local d = coroutine.create(D)
    --print("I am resuming in B")
    while true do
        for i=1,10 do
            print('B')
            if i==5 then
                print(coroutine.resume(d,1,2,3))
            end
        end
        --print("Now yielding B in script")
        coroutine.yield()
        --print("Resuming B again")
    end
end

c = coroutine.create(co)
print("\nCoroutine created")

coroutine.resume(c)

while true do
    for i=1,2 do 
        print('A')
    end
    coroutine.resume(c)
end

script2:

while true do print('C') end



So as you see in script 1 even the coroutine creates and calls a coroutine. The whole system works good in my initial tests. To make this work I wrap the original coroutine functions in my own functions. Which pass data between resume and yield and which prevent the C program debug hook from yielding if the yield from the script is hapenning. Doing these 2 things the sequence works correctly. The initial part of the output is attached in the text file



On Tue, Apr 1, 2014 at 5:39 AM, Thomas Jericke <tjericke@indel.ch> wrote:
On 04/01/2014 12:15 PM, Dominique Torette wrote:

Hi Thomas,

 

Thanks for the link to Lua 2013 workshop slides from Thomas Jericke (http://www.lua.org/wshop13/Jericke.pdf).

The solution explained starting from page 18 is close to what I tried (yield from a count debug hook).

But I’m not confident in such solution when your Lua programs also work with coroutines.

My idea is to suppress the coroutines support.

 

Thanks, Dominique Torette.

 

Yes this is true, you may not load the coroutines standard library to the interpreter. I certainly don't do that (and I also don't load the debug library either). Instead you may write your own API for the user to spawn coroutines and later synchronize them again.
I also introduced such things as mutex and sepmaphore, as they are needed as soon as you have pre-emption.
--
Thomas

C
C
C
C
C
C
C
C
C

Coroutine created
C
C
C
C
C
C
C
B
B
C
C
B
B
B
C
C
C
C
D
C
C
C
D
D
C
C
4       5       6
C
C
B
B
C
C
B
B
B
C
C
C
C
C
A
A
C
C
C
C
C
C
C
B
C
C
B
B
C
C
B
B
C
C
C
C
C
1       2       3
C
C
D
D
D
C
C
C
C
4       5       6
B
C
C
C
B
B
B
B
C
C
C
C
C
C