lua-users home
lua-l archive

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


Philippe Lhoste escribió:

Philippe Lhoste wrote:

Roberto Ierusalimschy wrote:

Producer Consumer          - Needs threading


Can't we use coroutines?


The test case writes about threads, but it wouldn't be the first time the rules be "abused"... :-)

I still didn't get around the coroutines spirit :-<, so I am not sure I can do this myself. But I would be interested to see the result :-)


Note: I think we can provide two implementations: one with pure Lua coroutines, and another with, say, LuaThreads.
Usage of external libraries is allowed in the rules.

My 2 cents from LuaTask universe:
-----------------------------------------------------------------
-- prodcom.lua
-- Producer/Consumer using LuaTask
-- Producer sends arg[1] messages to Consumer

if arg and arg[1] == 'Producer' then
   for i = 1, arg[3] do
       task.post( arg[2], string.format( 'Message %d', i), arg[3] - i)
   end
elseif arg and arg[1] == 'Consumer' then
   while true do
       local buf, flg, err = task.receive( -1)
       print( buf)
       if flg == 0 then
           task.post( arg[2], '', 0)
           break
      end
   end
else
   if not task then
       loadlib( 'task', 'luaopen_task')( )
   end
task.create( 'prodcom.lua', { 'Producer', task.create( 'prodcom.lua', { 'Consumer', task.id()}), arg[2] or 20})
   task.receive( -1)
end
-----------------------------------------------------------------