Reader and writer, producer and consumer

Source: Internet
Author: User

1. Written by readers

Written by: Write Data

Reader: Just read the data, there is no data to take away

Relationship between writer and writer: mutual exclusion

Relationship between reader and reader: no relationship

Relationship between writer and reader: synchronization, mutual exclusion


Workaround:

1) Readers first: readers are reading, the writer cannot terminate the reader until the reader is finished, the writer can write

2) Writing is preferred: The writer is writing, the reader cannot terminate the writer, until the writer finishes writing, the reader can write


2. Producer Consumers

Producer: Generating Data

Consumer: Take away data

Producer and producer: mutually exclusive relationship

Consumers and consumers: mutually exclusive relationships

Producer and Consumer: mutual exclusion, synchronous relationship


The primary role of the producer is to generate a certain amount of data into the buffer, and then repeat the process. At the same time, consumers consume the data in buffers. The key to this issue is to ensure that producers do not add data when the buffer is full, and consumers do not consume data when the buffer is empty.

To solve this problem, you must let the producer hibernate when the buffer is full, until the next time the consumer consumes the data in the buffer, the producer can be awakened and begin adding data to the buffer. Similarly, you can let consumers hibernate when the buffer is empty, wait until the producer adds data to the buffer, and then wake the consumer.


3. Difference

1) The data has been there, there is no vacancy to write, there is data to read the problem, the writer can overwrite the previous value, the reader will not consume data, the data will not disappear.

2) allows multiple readers concurrent access, while the consumer is mutually exclusive, write not only with the writer is mutually exclusive, but also with the reader also mutually exclusive, for the producer/consumer only with self-exclusion.


This article is from the "lovemeright" blog, make sure to keep this source http://lovemeright.blog.51cto.com/10808587/1827955

Reader and writer, producer and consumer

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.