"Turn" Lua coroutine not the same multithreaded programming idea

Source: Internet
Author: User

Lua Coroutine different multithreaded programming ideas Sunday, APR 26th, by Tim | Tags:coroutine, Lua

At the end of last week, reading the second edition of Lua programming, we now realize that there are two interesting points, one is the powerful table data structure, the other is coroutine. Perhaps the coroutine in Lua is a good design pattern, but my initial experience is that other languages and occasions can be very suitable for coroutine scenes.

First, Introduction

A synergistic program is similar to a thread, which is an execution sequence that has its own stack, local variables, and instruction pointers, while sharing global variables and most other things with other co-programs. The main difference between a thread and a co-worker is that a multithreaded program can run several threads at the same time, and the co-program needs to work cooperatively. That is, a program with multiple co-programs can run only one co-program at any time, and the running co-program will only be paused when its display is suspended.

Such as:

CO = coroutine.create (function () for i=1,10 Doprint ("Co", I) Coroutine.yield () endend)

Called from the main thread
Coroutine.resume (CO)
Prints 1 to 10, in turn

Second, the principle analysis
    • The so-called "threads" created by Coroutine are not real operating system threads, but are actually simulated by saving the stack state.
    • Because it is a fake thread, the overhead of switching threads is minimal, and the creation of threads is lightweight, and new_thread just creates a new stack of stacks for storing new coroutine in memory, also known as Lua_state

Lua_api lua_state *lua_newthread (lua_state *l)

    • Call yield () the current thread is handing over control, and can also return parameters through the stack. The thread that calls resume (which can be understood as the main thread) obtains the parameters returned.
    • Lua yield () is somewhat similar to Thread.yield () in Java, but the difference is greater. After the yield call in Java only switches the current CPU to another thread, the CPU may continue to return to thread execution at any time.
    • I prefer to compare the yield () and resume () in Lua with the Wait () and notify () in Java. They behave in a basic and consistent form.
    • The stack implementation is also available in the Yufeng (Erlang Master) analysis article on how LUA Coroutine is implemented?
Third, why Coroutine?

Coroutine has a basic understanding of the above, so everyone will like me to think, why use Coroutine? First study the advantages

    • Each coroutine has its own private stack and local variables.
    • At the same time, only one coroutine is executing without locking the global variable.
    • Sequential control, complete sequence of program execution. When the usual multithreading is started, the timing of its operation is unpredictable, which often makes it difficult to test all the situations. Therefore, the use of coroutine to solve the situation should give priority to using Coroutine.

Looking at the shortcomings and studying coroutine shortcomings, I looked for some explanations of why LUA implemented Coroutine. Several reasons have been explained in the paper coroutines in Lua (pdf) written by Brazilians:

    • LUA is implemented in ANSI C, and ANSI C does not include the implementation of thread, so if you want to increase the support for the thread in Lua, use the local implementation of the operating system, which can cause common problems. It also makes LUA bloated. So Lua chooses the coroutine implemented on ANSI C.
    • One of Lua's main design goals was to call C, which would cause confusion in the C call state if there were multiple threads within LUA, while only providing a coroutine level of suspension would keep the state consistent.

All of these reasons are used for specific reasons in Lua, and are not a very common cause. We also learned that Coroutine is actually an ancient design pattern that has been stereotyped in the 60 's, but modern languages rarely pay attention to this feature, and now can be exemplified by Windows fibers, Python's generators

Iv. Lua Coroutine and Erlang

There are 1 advantages above, that is, each coroutine has its own private stack and memory variable space. So it can be thought that the process in Coroutine and Erlang is very similar. But Coroutine can only be executed at the same time, and I think it's very similar to Erlang if it allows him to run multiple runs at the same time.

The implementation of the second edition of Lua programming 30.2 is to enable multiple C threads to start, then each C thread initiates a coroutine (like Erlang process) and then passes the variable values through the stack (like Erlang Process message) so that a process model like Erlang can be implemented. Since coroutine can actually be implemented in any language, the same design method should be implemented in other languages as well.

V. LUA OTHER

Lua is currently used primarily in the field of game programming, with the usual view that Lua is the glue language. Used to glue the various modular functions together. In terms of some of the code I'm reading, C and Lua are usually mixed together and have no explicit boundaries. For me a layman's vision I don't know what is doing C and what is calling Lua. In particular, if the "glue" is put too much, the independence of each module in the system will be affected. For example, the cloud-inspired Lua is not a C + + also mentioned, "This is a thick adhesive layer, it is absolutely necessary to abandon."

In addition [email protected] A [online game design] a little sentiment also mentioned to simplify the call, I summed up its view of the main two points:

    1. Do not have a redundant relationship, to a part of the management is good. (Managed by Lua/python)
    2. Adhesive layer (Lua/python interface) not too fat, we can introduce an "indirect layer" to make the adhesive layer "thin"

While Lua's efficient and streamlined design is highly praised, its performance rankings are not high, and Python is roughly at the same level. In addition, the "glue language" positioning also hindered its development in more areas.

"Turn" Lua coroutine not the same multithreaded programming idea

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.