lua-users home
lua-l archive

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


> ...
> Given the following code:
>
> 1 x = 0
> 2
> 3 prod = coroutine.create(
> 4 function()
> 5     while true do
> 6         put(x)
> 7         x = x + 2
> 8     end
> 9 end)
> 10
> 11 cons = coroutine.create(
> 12 function(p)
> 13     while true do
> 14         get(p)
> 15     end
> 16 end)
> 17
> 18 function put(x)
> 19     coroutine.yield(x)
> 20 end
> 21
> 22 function get(p)
> 23     local status, value = coroutine.resume(p)
> 24     return value
> 25 end
> 26
> 27 coroutine.resume(prod)
> 28 coroutine.resume(cons, prod)
> 29 print(coroutine.status())
>
> What is the output?
>
> a) dead
> b) running
> c) suspended
> d) There is no output.  (on the test, this is a different font so that you
> can see it is an answer, not a quote of output)
> e) An error occurs. (ditto)
>
> I'd give the answer, but programmers tend to love puzzling these things
out
> for themselves.  :)  Can you figure it out without running it?
>

I put it and run.. in my case the program frezze!! :(  If I put 'print'
statement inside 'get' function the program run and raise an error.  ?¿?¿
What can be happening?

function get(p)
    print("Executing get") -- puting it don't frezze
    local status, value = coroutine.resume(p)
    return value
end

Tales Aguiar