coroutines.nut 625 B

12345678910111213141516171819202122232425
  1. function coroutine_test(a,b)
  2. {
  3. ::print(a+" "+b+"\n");
  4. local ret = ::suspend("suspend 1");
  5. ::print("the coroutine says "+ret+"\n");
  6. ret = ::suspend("suspend 2");
  7. ::print("the coroutine says "+ret+"\n");
  8. ret = ::suspend("suspend 3");
  9. ::print("the coroutine says "+ret+"\n");
  10. return "I'm done"
  11. }
  12. local coro = ::newthread(coroutine_test);
  13. local susparam = coro.call("test","coroutine"); //starts the coroutine
  14. local i = 1;
  15. do
  16. {
  17. ::print("suspend passed ["+susparam+"]\n")
  18. susparam = coro.wakeup("ciao "+i);
  19. ++i;
  20. }while(coro.getstatus()=="suspended")
  21. ::print("return passed ["+susparam+"]\n")