lua-users home
lua-l archive

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


I would like to have a coroutine that automatically yields at every N
instructions or at some other regular interval. However, this appears to
be impossible in practice with Lua5. Various techniques I (and others
who've reported on this) have tried just result in the "attempt to yield
across metamethod/C-call boundary" error. 

Paul



----------------------------------------------------------
Well, think of coroutines as a way to be able 
to leave a function and return to it later at 
the exact same spot where you left it. 
It is especially useful for returning back to 
a long-running loop. It reduces the need for
call-back functions. I only recently discovered
how powerful coroutines are myself. 

A very simple example illustrates this:

function tryyield()
  print("First yield")
  coroutine.yield()
  print("Second yield")
  coroutine.yield()
  print("Third yield")
  coroutine.yield();
  print("All done");
end

co = coroutine.create(tryyield)

coroutine.resume(co);
coroutine.resume(co);
coroutine.resume(co);
coroutine.resume(co);
----------------------------------------------------------