Lua study notes coroutine (coroutine)

table of Contents

1. Blog introduction

2. Content

(1) What is a coroutine (taken from the rookie tutorial)

(2) The difference with threads (taken from the rookie tutorial)

(3) Method (taken from the rookie tutorial)

(4) Specific usage

3. Push

4. Conclusion


1. Blog introduction

As one of the Lua study notes, this article records the relevant knowledge points about Lua coroutines. The content of this article will be very simple. It is only an entry-level study for those who do not know the knowledge of coroutines. Students who want to go deeper can skip it.


2. Content

(1) What is a coroutine (taken from the rookie tutorial)

It has an independent stack, independent local variables, independent instruction pointers, and at the same time shares global variables and most other things with other cooperative programs.

(2) The difference with threads (taken from the rookie tutorial)

A program with multiple threads can run several threads at the same time, but cooperative programs need to run in cooperation with each other. Only one cooperative program is running at any given time, and this running cooperative program will only be suspended when it is explicitly required to be suspended. Coroutines are a bit similar to synchronized multithreading, and several threads waiting for the same thread lock are a bit similar to collaboration.

(3) Method (taken from the rookie tutorial)

method description
coroutine.create() Create coroutine, return coroutine, the parameter is a function, when used in conjunction with resume, the function call will be awakened
coroutine.resume() Restart coroutine and use it with create
coroutine.yield() Suspend coroutine, set coroutine to suspended state, this can have many useful effects when used in conjunction with resume
coroutine.status() Check the status of coroutine.
Note: There are three statuses of coroutine: dead, suspended, and running. Please refer to the following program for specific when there is such a status
coroutine.wrap() Create a coroutine, return a function, once you call this function, enter the coroutine, and repeat the create function
coroutine.running() Return the coroutine that is running. A coroutine is a thread. When running is used, it returns the thread number of a corouting.

(4) Specific usage

We can create a coroutine through create. The parameter of create is a function. Create returns a thread. We can call this thread through resume to execute the coroutine we created through create, as shown in the following code:

--创建
local co = coroutine.create(function()
	print("我被调用了")
end)

--启动
coroutine.resume(co)   

-----------------------------------输出
我被调用了

When a coroutine is running, there is no way to stop it from the outside. Only by yielding inside the coroutine can the coroutine be suspended, or called suspend, as shown in the following code:

--创建
local co = coroutine.create(function()
	for i=1,10 do
		print("输出:"..i)
		if i == 5 then
			coroutine.yield()
		end
	end
end)

--启动
coroutine.resume(co)   

-----------------------------------输出
输出:1
输出:2
输出:3
输出:4
输出:5

At the same time, when a coroutine is suspended, we can continue to execute the subsequent content of the coroutine by calling thread again through resume. The code is as follows:

--创建
local co = coroutine.create(function()
	for i=1,10 do
		print("输出:"..i)
		if i == 5 then
			coroutine.yield()
		end
	end
end)

--启动
coroutine.resume(co)   

----------被挂起后继续执行

--恢复
coroutine.resume(co)   

-----------------------------------输出
输出:1
输出:2
输出:3
输出:4
输出:5
输出:6
输出:7
输出:8
输出:9
输出:10

The above is basically all the contents of the coroutine. Let’s talk about the wrap method. The wrap method is similar to create. It also creates a coroutine. The difference from create is that wrap returns not a thread but a function itself, which is created by wrap. For the coroutine, we no longer call and resume through resume, but directly implement suspend and resume by calling the returned function itself. The code is as follows:

--创建
local co = coroutine.wrap(function()
	for i=1,10 do
		print("输出:"..i)
		if i == 5 then
			coroutine.yield()
		end
	end
end)

--调用  ---------将在输出五次后被挂起
co()

print("----------------------:被挂起")

--恢复
co()

-- -----------------------------------输出
输出:1
输出:2
输出:3
输出:4
输出:5
----------------------:被挂起
输出:6
输出:7
输出:8
输出:9

3. Push

Github:https://github.com/KingSun5


4. Conclusion

If you feel that the blogger’s article is well written, you may wish to pay attention to the blogger and like the blog post. In addition, the ability of the blogger is limited. If there is any error in the article, you are welcome to comment and criticize.

       QQ exchange group: 806091680 (Chinar)

       This group was created by CSDN blogger Chinar, recommend it! I am also in the group!

       This article is an original article, please reprint the source of the famous author and stick to the top! ! ! !

Guess you like

Origin blog.csdn.net/Mr_Sun88/article/details/105877427