When interviewed multithreaded modeling questions, there are two models that are frequently asked:
- producer/consumer model
- 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¬ify 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!