Producer Consumer & Reader writer

Source: Internet
Author: User

producer Consumers

For the problem of producer consumers, we can use a simple example of life to illustrate:

There was a plate on the table, a man put apples on the plate, a man took apples from the plate, the apples were the equivalent of the producers, the apples were the equivalent of the consumers, and the plates were the equivalent of a production place. If we want to make sure the apples are always in the apple, let the apple people keep them, and be faster than those who take the apples. When the plate is full, the person who put the Apple will wait for the Apple to take the apple, and so on the plate has a position, you can continue to put the apples. If we ask for only one apple at a time, the person who takes the Apple can only take one apple at a time, and the person who puts the apple and takes the apple is not only a person, so that the apple and put the apple between the people will have a mutually exclusive relationship, when a person put an apple, the other people can not put the apple, the same When a man takes an apple, no one else can take it. And the Apple and the person who put the Apple also have mutually exclusive relationship and synchronization relationship, put the apple after the person put the Apple, to inform the people take apples to take apples, otherwise the plate is full, the person who put the Apple will wait.

I'm drunk on a full-screen apple.


All right, let's not say apple.

What the producers have to do is produce data, and the consumer has to read the data and take it away. And producers and consumers are in a ring buffer, as I wrote in the pipeline blog.


Relationship between producer and consumer

(1) Consumers and consumers are mutually exclusive, because when a consumer reads the data, other consumers are unable to read the data.(2) producers and producers are mutually exclusive, as a producer cannot write data when it is written by another producer.(3) producers and consumers are mutually exclusive relations, synchronous relationship, when the producer produced, will inform consumers to consume, otherwise when the production of producers full, it is necessary to wait for consumers to consume, wait until the production requirements to meet the time of production, which for critical resources access will produce a certain sequence, Will increase the read speed of resources within the system.
consumers point to a location where there are valid data, the producer points to the location is not valid data, and the pipe ring buffer similar to the pipe of the ring buffer to take a look at understanding it. in this picture, you can think of write as a producer, in the production of data, read can be seen as a consumer, has been reading data. The resources that the producer cares about are the vacant positions in the annular buf, that is, where there is no valid data,consumers do not care about lattice, only care about the data


Reader Writer's question
What do readers do? The writer throws the data into the buffer, the reader reads the data, and does not take the data away .
Reader-writer relationship:(1) Readers and readers are not related, because the data will not be taken away, there will be no contention for resources, so anyone can read the same data,(2) The writer and the writer are mutually exclusive relations, when one writes, the other writer cannot write(3) The reader and the writer are mutually exclusive relationships, synchronous relationships, similar to producers and consumers
Reader-writer questions:(1) Many readers, write only one time, the reader will continue to read the data, the writer does not have the opportunity to write as data, will appear the writer Hungry(2) write a lot, readers only one time, the writer will continue to write data, readers do not have the opportunity to write data, the reader will appear hungry
Solutions for writer Hunger and Reader hunger:
(1) Write first: When there are many readers, when the writer comes, the subsequent readers will not be able to read, and so the reader is currently reading, and then let the writer write first, other readers can not read(2) Readers first: when there are many people who write, come to a reader, when the current writer finished writing to let the reader read first, read and then let the other writer write
about Read and write locks:(1) Read-only lock, is the behavior of the reader, when there is a writer, the writer will not write operations, the writer detects that the reader has read the lock (Pthread_rwlock_tryrdlock (&rwlock)! =0) waits until the reader is no longer read, and if no read-only lock (Pthread_rwlock_tryrdlock (&rwlock) ==0) is detected, it is written directly(2) Other locks are based on hangs, and when the request for a lock is not applied it will be suspended waiting for other processes to release the lock(3) read-write lock is based on spin lock, request lock resources, if the lock resources are ready, will be immediately occupied, if the lock resources are not ready, will always spin the application
Spin Lock: Saves the cost of suspend and wake-up when requesting resources without being suspended
If you stay in a critical resource for a long time, you need to use a mutex or a semaphore













Producer Consumer & Reader writer

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.