Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the different multithreaded programming idea of Lua coroutine?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, the editor will bring you about the different multithreaded programming ideas of Lua coroutine. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.

Coroutine in Lua is a good design pattern, but from my initial experience, I didn't expect that other languages and situations would be very suitable for coroutine scenarios.

I. brief introduction

A collaborative program is similar to a thread, that is, an execution sequence, with its own independent stack, local variables and instruction pointers, while sharing global variables and most other things with other collaborative programs. The main difference between threads and collaborative programs is that a program with multiple threads can run several threads at the same time, while collaborative programs need to run cooperatively with each other. That is to say, a program with multiple collaborative programs can only run one collaborative program at any time, and the execution of the running collaborative program will be suspended only when it is suspended.

Such as:

Co = coroutine.create (function () for iTunes 1) 10 doprint ("co", I) coroutine.yield () endend)

Called from the main thread

Coroutine.resume (co)

Will print 1 to 10 in turn.

Second, analysis of the principle

The so-called "threads" created by coroutine are not real operating system threads, but are actually simulated by saving stack state.

Because it is a fake thread, the overhead of switching threads is minimal, and creating threads is lightweight. New_thread just created a new stack variable in memory to store the new coroutine, also known as lua_State.

LUA_API lua_State * lua_newthread (lua_State * L)

Call yield () the current thread to hand over control and return parameters through stack. The thread that calls resume (which can be understood as the main thread) gets the returned parameters.

Lua yield () is somewhat similar to Thread.yield () in Java, but the difference is bigger. The yield call in Java simply switches the current CPU to another thread, and the CPU may continue to execute back to the thread at any time.

I prefer to compare yield () and resume () in Lua with wait () and notify () in Java. Their behavior is basically the same.

For the implementation of stack, you can also refer to the analysis article of Yufeng (Erlang master). How is lua coroutine implemented?

Third, Why coroutine?

I have a basic understanding of coroutine, so everyone will think like me, why use coroutine? Study the advantages first.

Each coroutine has its own private stack and local variables.

Only one coroutine is executing at a time, and there is no need to lock global variables.

The sequence is controllable, and the order of execution is completely controlled by the program. Normally, once a multithread is started, its runtime is unpredictable, so it usually makes it difficult to test all the situations. Therefore, coroutine should be given priority when coroutine can be used to solve the problem.

Looking at the disadvantages, before looking at the shortcomings of coroutine, I looked for some instructions on why coroutine is implemented in Lua. Several reasons are explained in paper Coroutines in Lua (pdf) written by Brazilians:

Lua is implemented by ANSI C, and ANSI C does not include the implementation of thread, so if you want to add thread support in Lua, you have to use the local implementation of the operating system, which will cause general problems. It also makes Lua bloated. So Lua chose coroutine, which is implemented on ANSI C.

One of the main design purposes of Lua is to call C, if there is a multithreaded implementation within Lua, it will cause confusion in the state of C calls, while only providing suspension at the coroutine level can maintain the consistency of the state.

All of the above reasons are based on Lua special reasons, not very general reasons. We also know that coroutine is actually an ancient design pattern, which has been stereotyped since the 1960s, but modern languages pay little attention to this feature. Examples can be given at present, such as Windows's fibers and Python's generators.

4. Lua coroutine and Erlang

One of the advantages above is that each coroutine has its own private stack and memory variable space. Therefore, process in coroutine and Erlang can be considered to be very similar. But coroutine can only be executed by one at a time, and if he can run more than one at the same time, I think it will be very similar to Erlang.

Lua programming introduces an implementation method that allows multiple c threads to start, then each c thread starts a coroutine (similar to Erlang process), and then passes a variable value (similar to Erlang process message) through stack, so that an Erlang-like process model can be implemented. Since coroutine can be implemented in virtually any language, other languages should be able to implement the same design approach.

5. Lua others

Lua is currently mainly used in the field of game programming, and the general view is that Lua is a "glue language". Used to glue together the various modular functions. From some of the code I've read so far, C and Lua are usually mixed together with no clear boundaries. From a layman's point of view, I can't tell what is doing C and which is calling Lua. In particular, if this "glue" is put too much, the independence of each module in the system will be affected. For example, Yunfeng's Lua is not C++ also mentioned, "this belongs to the too thick adhesive layer, is absolutely necessary to discard."

In addition, Code@Pig 's article "online Game Design" also mentions the need to simplify the call. I summarize its two main points:

Don't have a redundant relationship, just give a part responsible for management. (managed by Lua/python)

The adhesive layer (Lua/python interface) should not be too fat. We can make the adhesive layer "thin" by introducing an "indirect layer".

While Lua is praised for its efficient and streamlined design, its performance rankings are not high, roughly on a par with Python. In addition, the positioning of "glue language" also hinders its development in more fields.

This is what the editor shares with you about Lua coroutine's different multithreaded programming ideas. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report