루아는 coroutine을 지원한다. (이것은 세미-coroutine, 제너레이터, 또는 협력적 멀티쓰레딩이라고도 불린다.) 루아에서의 coroutine은 독립적인 실행 쓰레드를 의미한다. 다만, "실제" 쓰레드와는 달리 coroutine은 yield 함수를 호출하는 것에 의해 단지 그 실행을 지연할 뿐이다.
coroutine.create를 호출함으로써 coroutine을 생성할 수 있다. 인자는 한개만 필요하며, coroutine의 메인 함수를 지정하면 된다. coroutine.create은 단지 신규 coroutine을 생성하고 그것으로의 핸들(thread 타입)을 반환할 뿐이다. 이 함수는 coroutine의 실행을 시작하지는 않는다.
coroutine.create 함수로 부터 반환된 쓰레드를 인자로 받은 coroutine.resume함수를 처음 실행할 때, coroutine은 지정한 함수의 첫번째 라인부터 실행되기 시작한다. coroutine.resume에 넘겨진 추가적인 인자값들은 coroutine 메인 함수의 인자로서 설정된다. coroutine이 일단 실행되기 시작한 후에는 yield되거나 소멸할때까지 계속 실행된다.
A coroutine can terminate its execution in two ways: Normally, when its main function returns (explicitly or implicitly, after the last instruction); and abnormally, if there is an unprotected error. In the first case, coroutine.resume returns true, plus any values returned by the coroutine main function. In case of errors, coroutine.resume returns false plus an error message.
A coroutine yields calling coroutine.yield. When a coroutine yields, the corresponding coroutine.resume returns immediately, even if the yield happens inside nested function calls (that is, not in the main function, but in a function directly or indirectly called by the main function). In the case of a yield, coroutine.resume also returns true, plus any values passed to coroutine.yield. The next time you resume the same coroutine, it continues its execution from the point where it yielded, with the call to coroutine.yield returning any extra arguments passed to coroutine.resume.
The coroutine.wrap function creates a coroutine like coroutine.create, but instead of returning the coroutine itself, it returns a function that, when called, resumes the coroutine. Any arguments passed to that function go as extra arguments to resume. The function returns all the values returned by resume, but the first one (the boolean error code). Unlike coroutine.resume, this function does not catch errors; any error is propagated to the caller.









![[http]](/wiki/imgs/http.png)
