On the process of association

Source: Internet
Author: User

Article Source: http://blog.csdn.net/lanphaday/archive/2010/03/19/5397038.aspx

The process, also known as micro-threading and fiber, is said to originate in the Simula and Modula-2 languages (I did not delve into, wrong, please correct), modern programming languages are basically supported, such as Lua, Ruby and the latest Google Go, of course, but also the recent very impressive Falcon. The process is a user-space thread, the operating system has no knowledge of its existence, so users need to do their own scheduling, to run a collaborative multi-tasking is very appropriate . Its useful way to do things, with threads or processes is usually the same as can be done, but often many more lock and communication operations.

The following is a preemptive multi-threaded programming implementation of the producer consumer Model (PSEUDOCODE):
//Queue container var Q: = new queue//consumer thread loop Lock (q) Get item from Q unlock (q) If item with this item else sleep// Producer Thread loop create some new items lock (q) Add the items to Q unlock (q)

The above code can see that the thread implementation has at least two points of mishap:

1, the operation of the queue requires an explicit/implicit (using thread-safe queue) lock operation.

2, the consumer thread also through sleep to the CPU resources timely "humility" to the producer thread use, the timely is how long, basically only can use the experience value statically, the effect is often not from the understanding.

And the use of the process can be a better solution to take a look at the implementation of the producer consumer model based on the process (pseudo-code)://Queue container var Q: = new queue//producer Coprocessor Loop while Q was not full create some n EW items Add the items to Q yield to consume//consumer coprocessor Loop while q are not empty remove some items from Q use the Items yield to produce

You can see from the above code before the locking and humility CPU's mishap no longer exist, but also lost the ability to take advantage of multi-core CPU . So choose the thread or the process, it depends on the application. Here is a brief talk about the common use of the process, one of which is the state machine, can produce more readable code, there is a parallel role model, which is more common in game development, as well as the generator, to facilitate the general traversal of input/output and data structures.

Although the process is so good, it's been a long time since there are not many languages in the actual language or library that support the process because of the limitations imposed by the stack-based subroutine implementation, so the thread is widely accepted as a substitute (and of course, the thread has its way beyond the path). Today, however, many languages have built-in support for the process, even in C/s + + languages. MS Windows 2000 later version number, all support so-called Fiber, that is, fiber, in fact, is the term of the association, in the open source platform, the POSIX standard also defines the standard of the association, GNU Portable Threads implements a cross-platform user space thread, There is also a nickname for the Association process. In this season of flowering, it is the time for us to learn and use it well.

On the process of association

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.