' Producer-consumer ' model and ' reader-writer ' model

Source: Internet
Author: User

★ Producer-Consumer model

First of all, we analyze the producer and consumer models: producers and consumers are indispensable in the model of 2 roles, of course, the model must need a place to save data, can be produced by producers of data storage. At the same time, the model must meet the producers to produce data, consumers can use, that is, consumers must be located in the producer, of course, producers of data produced by the most places will not be able to continue production, the following is a simple diagram:


650) this.width=650; "title=" Untitled. png "src=" http://s2.51cto.com/wyfs02/M01/85/31/ Wkiom1ecxaoq36apaaayvae60ou407.png "alt=" Wkiom1ecxaoq36apaaayvae60ou407.png "/>

Of course, if there are multiple consumers and multiple producers, the relationship between producer and consumer is synchronous, and the producer is mutually exclusive with the producers, because a space cannot be produced simultaneously by multiple producers. Consumers and consumers are also mutually exclusive, but also to ensure that the data in a space can not be consumed at the same time.


Whether producers are able to produce depends on how much space they have, and whether consumers can consume depends on how much data they have, where producers have space at the start, and no data is produced, so producers must be living before consumers.


Example: The following is a concrete implementation of the producer-consumer model, which uses a linked list to store the production data, the production of the data header into the linked list, the consumer's consumer data to be deleted by the head.

650) this.width=650; "title=" Untitled. png "src=" http://s4.51cto.com/wyfs02/M01/85/32/ Wkiom1eca8nrmuawaaczavhguee503.png "alt=" Wkiom1eca8nrmuawaaczavhguee503.png "/>

650) this.width=650; "title=" Untitled. png "src=" http://s1.51cto.com/wyfs02/M02/85/32/ Wkiol1ecbmlsw86uaacy-nx6-qg019.png "alt=" Wkiol1ecbmlsw86uaacy-nx6-qg019.png "/>

650) this.width=650; "title=" Untitled. png "src=" http://s2.51cto.com/wyfs02/M00/85/32/ Wkiom1ecbrddjqdzaaa3dcwyak8603.png "alt=" Wkiom1ecbrddjqdzaaa3dcwyak8603.png "/>

Operation Result:

650) this.width=650; "title=" Untitled. png "src=" http://s3.51cto.com/wyfs02/M02/85/32/ Wkiom1ecbzbh08biaabgkmdczq8584.png "alt=" Wkiom1ecbzbh08biaabgkmdczq8584.png "/>


★ Read and Write Model

The reading and writing model is similar to the producer's consumer model, but there are different places where multiple readers can share data in a piece of space, which is mutually exclusive between the reader and the writer.

Read-write Lock: Also called spin lock, a read-write lock allows multiple readers to read resources, but only allows one writer to manipulate the data.


Cases:

650) this.width=650; "title=" Untitled. png "src=" Http://s2.51cto.com/wyfs02/M00/85/33/wKioL1ech-CAZrJfAACs_ 87paq8872.png "alt=" Wkiol1ech-cazrjfaacs_87paq8872.png "/>

650) this.width=650; "title=" Untitled. png "src=" http://s2.51cto.com/wyfs02/M01/85/33/wKioL1eciCWQP___ Aacjw7pj0ww209.png "alt=" Wkiol1ecicwqp___aacjw7pj0ww209.png "/>

650) this.width=650; "title=" Untitled. png "src=" http://s5.51cto.com/wyfs02/M02/85/33/ Wkiom1ecihxjq8gyaaaextwhw3q444.png "alt=" Wkiom1ecihxjq8gyaaaextwhw3q444.png "/>

Operation Result:

650) this.width=650; "title=" Untitled. png "src=" http://s3.51cto.com/wyfs02/M00/85/33/ Wkiom1ecixkyyvj5aabji5zqopu896.png "alt=" Wkiom1ecixkyyvj5aabji5zqopu896.png "/>


This article from "Unintentional persistent" blog, declined reprint!

' Producer-consumer ' model and ' reader-writer ' model

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.