We learned producer/consumer pattern during the class but only with the example of one producer and one consumer. In our homework, we are asked to do the following:
Write a program that generates random strings, then sorts them alphabetically using the producer/consumer pattern. Create 1000 strings and use a bounded queue of size 100. Use 100 producers and 10 consumers.
Can anyone explain to me the benefit of implementing multiple producers and consumers instead of single producer and consumer in this situation? I assume multiple producers can generate multiple random strings, but what are multiple consumers designed for? To remove strings quicker from the queue and then put them to another array to sort?
Thank you.
According to the professor's reply, I think the assignment is asking us to sort each string alphabetically, like: "aagpqhtpqht" -> "aaghhppqqtt"
, instead of what I understood as "puatn", "qupango", "agnqotu", "bowtn" -> "agnqotu", "bowtn", "puatn", "qupango"
.
So I assume consumer's role is to take one string at a time from the queue, then sort THE string.
There's no benefit of using multiple consumers here for sorting strings alphabetically in this situation. It's supposed to be a practice for implementing producer/consumer pattern using multiple producers and consumers.