The Coroutine, thecoroutine

Source: Internet
Author: User

The Coroutine, thecoroutine
About Coroutine

Speaking of coroutine, subroutine is a common function. Call a function to start execution, and then exit after the function execution is complete. When the function is called again, it starts from the beginning and there is no save status between calls; however, coroutine can be called again upon exit, and can be executed from the last exit point. That is to say, the call of coroutine is saved. If there are multiple coroutines, you can call them repeatedly, but the two coroutines move forward with each other. When a coroutine exits, it does not return, but yield, indicating that the current execution right is handed over to the next coroutine for processing, or a result value is handed over. Coroutine is a bit similar to the commonly used thread. A thread will execute a function we specified, and then when the function calls a blocking IO or synchronously waiting for an event or message, it will be scheduled by the operating system to the waiting queue. After the completion or event response, the thread will continue to execute from the waiting place. The biggest difference between thread and coroutine is the scheduling method. The former is usually preemptible Scheduling Based on Time slices, while coroutine is collaborative scheduling.

In other words, the commonly used functions can be easily converted to coroutine. Coroutine also has a special case, that is, generator. generator can be executed from the last returned place like coroutine. It can also be run multiple times in yield or suspend itself, however, general coroutine can specify the coroutine to be executed in yield, while generator cannot. Therefore, generator is generally used to implement the iterator.

Coroutine can be easily used to implement a state machine. Coroutine implements State opportunities to make the code more readable. For example, to implement an SMTP client using the event-driven paradigm, You need to record the status of each processing and the code becomes abnormal and complex. Suppose you still use the event-driven paradigm implementation, but you use coroutine, the internal process of coroutine is the general SMTP process, but you need to exit after each request is sent, then, when the response packet is received, the event-driven engine calls coroutine again and goes directly to the Last Exit. Does it feel more refreshing?

Coroutine is much more lightweight than thread, and thread is implemented at the operating system level. Scheduling involves switching the CPU context, which is more important than coordinated scheduling of coroutine, in highly concurrent scenarios, it is very inefficient to use thread to carry each request, but coroutine is easy to implement at the language or database level, so the scheduling cost is much lower. The coroutine implementation like Lua requires programmers to call yield for their own scheduling, which is troublesome. However, assuming that languages like Golang and Erlang hide the displayed cooperative scheduling, programmers can focus on function implementation, it is easier to accept. In golang, coroutine is called goroutine. When a blocking operation or Channel read/write operation is called in goroutine, the corresponding goroutine is executed, after the task is completed, the task is scheduled to run again. This Synchronous Programming method simplifies the concurrent scenario and makes the logic clearer. Golang's implementation hides general explicit Cooperative scheduling, which is more easily accepted by programmers.

Coroutine implemented in C Language

Generally, Coroutine is implemented in languages with GC mechanisms. For example, implementing coroutine in C/C ++ is complicated and troublesome, but there are still many different implementations, for example, the libtask library implemented by Russ Cox and boost in C ++. coroutine library and so on. Among them, I think the most bright version is the Duff's Device-based version "Coroutine in C" by Daniel Simon Tatham (author of putty). It shows how to simplify a complex decompressor with coroutine. Simon implemented two types of coroutine: one is based on the static variable storage state, and the other is to pass a pointer to the Save state through the parameter. The latter is more common. Click here for detailed code.

Boost. asio. coroutine

The coroutine implementation in asio is the same as the first implementation of Simon above. So there is not much to say. Boost. asio. coroutine is very easy to use. It contains coroutine. hpp and yield. hpp in its own code, and creates a class derived from the coroutine class. Then you can write the corresponding coroutine according to your business requirements. Of course, you can also combine the coroutine class into your own class as your member variables.

The author of asio also gave A detailed description of "A potted guide to stackless coroutines" and also wrote A special user guide "Composed operations, coroutines and code makeover".

 

Summary

To use Coroutine, we suggest using modern new languages, such as Golang, Erlang, and Scala.


Can Help Me Translate this English section (the description of the coroutine)

The Coordinated Program (coroutine) of the executable program can be suspended wherever the yield declaration is used. The yield return value specifies when coroutine is reused. Coroutines performs well when the model is built on multiple frameworks. Coroutines has almost no performance overhead. The StartCoroutine function always returns results immediately regardless of whether you get the correct results. This will wait until the cooroutine (couroutine, I suspect there is a mistake in the original article here, I translated it as coroutine) has completed the executable program.

Not very good. I have never touched on CEN. Hope to help you.

For lua: a [n], a = a, a [n] -- what is wrong? An error is reported in this line? The source code is as follows:

A [n], a = a, a [n]

Take a closer look at this parallel assignment. If a is a table, after the assignment is complete, a becomes its element (in this example, it is also a number)
 

Related Keywords:
Related Article

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.