Producer and consumer patterns
The thread pool
Simple decoupling of producers and consumers is not the essence of this design pattern
Setting this buffer is
- Decoupling always has a connection, and low coupling is not uncoupling, so buffers can accommodate the needs of both
- Synchronous support, since the function call is synchronous (or blocked), the producer has to wait until the consumer’s method returns. Buffers solve this problem and the producer will produce ahead of time. It is possible to have both producers and consumers use buffers through double buffers
- Support uneven busy, producer and consumer speed may not be consistent, so that surplus producers can be put in the buffer to slow down
Pipes also have this pattern
Advantages:
-
Almost all operating systems support the use of pipe characters in shell mode. So it’s easy to implement cross-platform.
-
Most programming languages can work with STDIO, so it’s easy to implement across programming languages.
3. Pipelining saves thread-safety trivia [the operating system does it for you]. It is beneficial to reduce the cost of development and debugging.
Of course, this approach has its drawbacks:
-
The producer and consumer processes must be on the same host and cannot communicate across machines. This disadvantage is more obvious.
-
This works well in one-on-one situations. But if you want to expand to one-to-many or many-to-one, that’s a little trickier. So the scalability of this approach should be compromised. This disadvantage becomes more apparent if similar extensions are to be considered in the future.
-
Because the pipe is created by the shell, it is not visible to the processes on either side (the program only sees stDIO). In some cases, this makes it inconvenient for the program to manipulate the pipe (such as resizing the pipe buffer). This disadvantage is less obvious.
-
Finally, this method can only send data one-way. Fortunately, in most cases, the consumer process does not need to pass data to the producer process. If you do need feedback (from consumers to producers), it’s hard. We may have to consider alternative IPC.
This article is participating in the “Nuggets 2021 Spring Recruitment Campaign”, click to see the details of the campaign.