Coroutines are similar to threads , It's an execution sequence , Own its own independent stack , Local variables and pointers , At the same time, it shares global variables and most other things with other collaborators .
Different from thread : A program with multiple threads can run several threads at the same time , And collaborative programs need to work collaboratively with each other .
That is to say, a program with multiple cooperative programs can only run one cooperative program at any time , And the running coroutine will only ask to suspend in the display (suspend) Time , Its execution will be suspended .
* Collaborative program foundation Lua Place all functions about the coroutine in a single named "coroutine" Of table in . function create For creating new collaborators ,
It has only one parameter , It's a function . This function code is the content that the cooperative program needs to execute . create Will return a thread Value of type , To represent a new collaboration program .
usually create Is an anonymous function . for example : co = coroutine .create(function () print("hi") end)
print( co ) --> thread: 0x8071d98 A coroutine can be in four different states ：
* Pending (suspended)
* function (running)
* death (dead)
* normal (normal) When creating a synergist , It is suspended . That is, the collaborator will not create it , Automatically execute its contents .
Through status To check the status of the coroutine print("coroutine.status(co)) --> suspended
function coroutine.resume Used to start or restart the execution of a coroutine , And change its status to operation ： coroutine.resume(co) --> hi
In this case , The content of the coroutine is simply printed "hi" And then it's over , And then it's dead . It will never return . print("coroutine.status(co))
-->dead Lua Collaborative processes also have intentional and useful mechanisms , It's a couple resume-yield To exchange data . On the first call resume Time ,
There is no corresponding yield To wait for it , So all of the resume The extra parameters of are regarded as the parameters of the main function of the cooperative program . co =
coroutine.create(function(a, b, c) print("co", a, b, c) end)
coroutine.resume(co, 1, 2, 3) --> co 1 2 3 Lua It provides a “ Asymmetric cooperative program （symmetric
coroutine)". in other words Lua Two functions are provided to control the execution of cooperative programs , One for pending execution , One for recovery execution .
Other languages offer “ Symmetric cooperative program (symmetric coroutine)", Only one of these functions is used to transfer the execution rights between cooperating programs .
* The Conduit (pipe)
* filter （filter)
* Non preemptive multithreading require "socket" host = "www.w3.org" file = "/TR/REC-html32.html"
Open a TCP connect , Connect to the 80 port c = assert (socket.connect(host, 80)
This operation will return a connection object , It can be used to send file requests . c: = send("GET" .. file .. " HTTP/1.0\r\n\r\n")
while true do local s, status, partial = c:receive(2^10) io.write(s or partial）
if status== “closed" then break end end c:close()