0
votes

When interviewed multithreaded modeling questions, there are two models that are frequently asked:

  1. producer/consumer model
  2. writer/reader model

My question is I can't catch the essential distinction between these two models.

What I understand for these two models is below: For producer/consumer model, producers until some halting criteria, at which it signals a consumer and waits on another condition variable while consumers wait until an item have been produced and then proceed to "consume it," notifying the producers that another slot is ready for production.

For writer/reader model, there are three key parameters applied(ref ): uses one mutex, two conditional_variable and three integers.

readers - readers in the cv readerQ plus the reading reader
writers - writers in cv writerQ plus the writing writer
active_writers - the writer currently writing. can only be 1 or 0.

For me, both of them use "mutex and condition variables", the only difference is producer/consumer wait&notify on conditional variables, while read/write uses conditional variables and integers together to check whether satisfied lock/unclock conditions or not.

I know one distinction is that for producer/consumer model, both producer and consumer would change the shared data, but they are disconnected from each other. They just communicate through the shared data (usually indicated by a queue).There is no need for producers/consumers to know whether there is an available consumer/producer, i.e the status of both parties is not important. However, in write/read model, both parties need to trace other partie's status (i.e.available number). BUT, I believe this is not the essential distinction.

Besides above naive understanding, could anyone help to tell me what are the essential distinctions between these two models? Thank you very much!

1
This question needs significant editing for clarity. I sort of understand OP's intent but it's just written very loopy.CinchBlue

1 Answers

1
votes

Well, they are actually quite irrelevant:

In very high level:

Producer/Consumer aims at having someone (Producers) producing data for processing. Someone else (Consumers) are waiting for data. Once the data-to-be-processed arrive, it will be consumed by one (and only one) Consumer. Then the Consumer owns the data and perform its work.

Reader/Writer is a way to lock a shared resource / data. Everyone are working against the same piece of data. However, we knows that sometimes the data needs to be modified, hence we want to work as Writers (hence get a Writer lock). Sometimes the data simply needs to be read, hence we want to work as Readers. The whole purpose of Reader-Writer-lock is to avoid unnecessary contention as Readers are only doing read-only operation on the resource.