Article Source: http://blog.csdn.net/lanphaday/archive/2010/03/19/5397038.aspx
Coroutine, also known as microthread and fiber, is said to be derived from the Simula and Modula-2 language (I did not go deep, wrong please correct), modern programming language is basically supported, for example, Lua, Ruby, and the latest Google go, and of course, Falcon, which has made me amazing recently.Coroutine is a user space thread, and the operating system has no idea about its existence. Therefore, you need to schedule it yourself to run collaborative multitasking.. It works with coroutine, which can also be done by threads or processes, but often requires a lot of locking and communication operations.
The following is a preemptible multi-threaded programming implementation (Pseudo Code) of the producer and consumer model ):
// Queue container var Q: = new queue // consumer thread loop lock (q) Get item from Q unlock (q) if item use this item else sleep // producer thread loop create some new items lock (q) Add the items to Q unlock (q)
The code above showsThreadThere are at least two hard points to achieve:
1. Explicit/implicit locking (using thread-safe queues) is required for queue operations.
2. The consumer thread also uses sleep to "modestly" CPU resources for use by the producer thread in a timely manner. How long is the proper time? Basically, the user experience can only be used statically, and the effect is often unsatisfactory.
The use of coroutine can better solve the problem. The following describes the implementation of the producer and consumer model based on coroutine (pseudo code ):// Queue container var Q: = new queue // producer coroutine loop while Q is not full create some new items add the items to Q yield to consume // consumer coroutine loop while Q is not empty remove some items from Q use the items yield to produce
We can see from the above Code that the previous locks and consortium make the CPU hard injury no longer exist, but alsoThe ability to utilize multi-core CPU is lost.. Therefore, the choice of thread or coroutine depends on the application scenario. The following briefly describes the common application of coroutine. One of them is the state machine, which can generate code with higher readability. The other is the parallel role model, which is more common in game development; and the generator, which helps to traverse the input/output and data structure.
Although coroutine is so good, it seems that for a long time, due to the limitations of stack-based subroutines, there are not many languages that support coroutine in actual languages or libraries, therefore, the thread is widely accepted as a replacement (of course, the thread also exists beyond the coroutine. However, today, coroutine support is built in many languages, even C/C ++. MS Windows 2000 later versions, are supported by the so-called fiber, that is, fiber, in fact is the name of the coroutine; in the open source platform, POSIX standards also define coroutine standards, GNU portable threads implements cross-platform user space threads, that is, the coroutine is also called. It is the time for us to learn and use it.