lua-users home
lua-l archive

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


Tales Aguiar writes:

"I put it and run.. in my case the program frezze!! ...(snip)...
What can be happening?"


The correct answer is d) There is no output.

This code is a simple producer-consumer relationship.  In this case there
are no conditions that will stop either the producer or the consumer so the
code will never reach line 29.  Consequently there will be no output.


Here is the code again if you missed it.
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)

 
Sincerely,
R. Grant Reed
High-Level Certifications