루아 코 루틴
시작하기 사례 1
- 코 루틴 생성 및 시작
- 코 루틴 함수 coroutine.create () 정의
- coroutine.resume () 시작
co=coroutine.create( --创建协程函数 function (a,b) print(a+b) end ) coroutine.resume(co,20,30) --启动协程函数
시작하기 사례 2
- coroutine.wrap ()을 통해 코 루틴을 만들고 시작합니다.
co=coroutine.wrap( function (a,b) print(a+b) end ) co(20,30)
시작하기 사례 3
- coroutine.yield ()를 사용하여 코 루틴을 중간에 일시 중단하고 다시 시작합니다. 두 번째로 coroutine.resume ()은 매개 변수를 전달할 필요가 없습니다.
co=coroutine.create( function (a,b) print(a+b) coroutine.yield() print(a-b) end ) coroutine.resume(co,1,2) print("I'm here!") coroutine.resume(co) 输出: 3 I'm here! -1
시작하기 사례 4
- coroutine.status ()
- 코 루틴에는 세 가지 상태가 있습니다 : dead, suspended, running
co=coroutine.create( function (a,b) print(a+b) print(coroutine.status(co)) --running print(a+b) coroutine.yield() print(a-b) end ) print(coroutine.status(co)) --此时未启动协程,suspended coroutine.resume(co,10,20) print(coroutine.status(co)) --suspended print("I'm here!") coroutine.resume(co) print(coroutine.status(co)) --dead
시작하기 사례 5
- coroutine.running ()
- 실행중인 코 루틴을 반환합니다. 코 루틴은 스레드입니다. running을 사용하면 코 루팅 스레드 번호를 반환합니다.
co=coroutine.create( function (a,b) print( coroutine.running() ) --thread: 00A78110 coroutine.yield() print(a-b) end ) print( coroutine.running() ) --nil coroutine.resume(co,10,40) print("I'm here!") coroutine.resume(co)
시작하기 6
- 코 루틴은 여러 값을 반환 할 수 있습니다. 첫 번째 값은 코 루틴이 성공적으로 시작되었는지 여부를 나타내는 부울 값이며 코 루틴 함수의 반환 값은 다음과 같습니다.
co=coroutine.create( function (a,b) coroutine.yield(a*b,a/b) return a%b,a/b+1 end ) res1,res2,res3 = coroutine.resume(co,10,40) print(res1,res2,res3) --true 400 0.25 print("I'm here!") res1,res2,res3 = coroutine.resume(co) print(res1,res2,res3) --true 10 1.25
요약하자면
방법 기술 coroutine.create () 코 루틴 생성, 코 루틴 반환, 매개 변수는 함수이며, resume과 함께 사용하면 함수 호출이 깨어납니다. coroutine.resume () 코 루틴을 다시 시작하고 create와 함께 사용하십시오. coroutine.yield () 코 루틴을 일시 중단하고, 코 루틴을 일시 중단 상태로 설정합니다. 이것은 재개와 함께 많은 유용한 효과를 가질 수 있습니다. coroutine.status () 코 루틴 상태보기
참고 : 코 루틴 에는 세 가지 상태가 있습니다 : dead, suspended, running. 이러한 상태가있는 경우 구체적으로 다음 프로그램을 참조하십시오.coroutine.wrap () 코 루틴을 생성하고 함수를 반환합니다.이 함수를 호출하면 코 루틴을 입력하고 생성 함수를 반복합니다. coroutine.running () 실행중인 코 루틴을 반환합니다. 코 루틴은 스레드입니다. running을 사용하면 코 루팅 스레드 번호를 반환합니다.
- 코 루틴은 하단에 스레드로 구현됩니다.
- 코 루틴이 생성되면 이벤트가 새 스레드에 등록됩니다.
- resume trigger 이벤트를 사용하면 create의 coroutine 함수가 실행되고 yield가 발생하면 현재 스레드가 일시 중지되어 resume trigger 이벤트를 다시 기다립니다.
포괄적 인 사례
function foo (a) print("foo 函数输出", a) return coroutine.yield(2 * a) -- 返回 2*a 的值 end co = coroutine.create(function (a , b) print("第一次协同程序执行输出", a, b) -- co-body 1 10 local r = foo(a + 1) print("第二次协同程序执行输出", r) local r, s = coroutine.yield(a + b, a - b) -- a,b的值为第一次调用协同程序时传入 print("第三次协同程序执行输出", r, s) return b, "结束协同程序" -- b的值为第二次调用协同程序时传入 end) print("main", coroutine.resume(co, 1, 10)) -- true, 4 print("main", coroutine.resume(co, "r")) -- true 11 -9 print("main", coroutine.resume(co, "x", "y")) -- true 10 end print("main", coroutine.resume(co, "x", "y")) -- false cannot resume dead coroutine
- 위의 예는 다음과 같이 계속됩니다.
- 재개를 호출하여 협력 프로그램을 깨우십시오. 재개 작업이 성공하면 true를 반환하고 그렇지 않으면 false를 반환합니다.
- 협력 프로그램 운영
- yield 문으로 실행합니다.
- Yield는 협업 프로그램을 일시 중단하고 첫 번째 이력서가 반환됩니다. (참고 : 여기서 yield는 반환되고 매개 변수는 이력서의 매개 변수입니다.)
- 두 번째 재개시 협력 프로그램을 다시 시작합니다. (참고 : 재개 매개 변수에서 첫 번째 매개 변수를 제외하고 나머지 매개 변수는 yield 매개 변수로 사용됩니다.) yield return;
- 협력 프로그램은 계속 실행됩니다.
- 사용 된 공동 프로그램이 계속 실행되고 완료 후에도 resume 메서드를 계속 호출하면 출력 : cannot resume dead coroutine
- 이력서와 수익률의 강력한 조합은 이력서가 기본 프로세스에 있고 외부 상태 (데이터)를 협업 프로그램의 내부 부분으로 전송하고 yield는 내부 상태 (데이터)를 기본 프로세스로 반환한다는 것입니다.
생산자-소비자 문제
local newProductor function productor() local i = 0 while true do i = i + 1 send(i) -- 将生产的物品发送给消费者 end end function consumer() while true do local i = receive() -- 从生产者那里得到物品 print(i) end end function receive() local status, value = coroutine.resume(newProductor) return value end function send(x) coroutine.yield(x) -- x表示需要发送的值,值返回以后,就挂起该协同程序 end -- 启动程序 newProductor = coroutine.create(productor) consumer() 输出: 1 2 3 4 5 6 7 8 9 10 11 12 13 ...
(루아 노트) : 루아 코 루틴
추천
출처blog.csdn.net/baidu_41388533/article/details/108512181
추천
행