协同的使用
1 local threads = {} 2 function test() 3 for i=1,10,1 do 4 coroutine.yield(i); 5 end 6 end 7 local co = nil; 8 9 for i=1,5,1 do 10 co = coroutine.create(test) 11 table.insert(threads,co) 12 end 13 14 local nCount = 0; 15 16 while true do 17 nCount = table.getn(threads); 18 if 0 == nCount then 19 break; 20 end 21 for k,v in ipairs(threads) do 22 local s,v = coroutine.resume(v) 23 if s then 24 if v then 25 print(k.."_____________________"..v.."___"..os.date()) 26 else 27 print(k.."____v is nil") 28 table.remove(threads,v) 29 end 30 end 31 end 32 end