We consider two processes, called “producer” and “consumer”, respectively. The producer is a cyclical process and each time it goes through its cycle it produces a certain portion of information, which must be processed by the consumer. The consumer is also a cyclical process and each time it goes through its cycle it can process the next piece of information, as it was produced by the producer. A simple example is given by a computational process, which produces as “portions of information” images of punched cards to be punched by a punched card, which plays the role of the consumer.[1]
A producer creates items and stores them in a data structure, while a consumer removes items from that structure and processes them.
If consumption is greater than production, the buffer (data structure) empties, and the consumer has nothing to consume
If consumption is less than production, the buffer fills up, and the producer cannot add more items. This is a classic problem called limited buffer.
Suppose we have a producer that publishes an email in the buffer, and a consumer that consumes the email from the buffer and displays a message stating that an email was sent with the new access password for the email. email provided.
package main import ( "fmt" "os" "strconv" "sync" "time" ) type buffer struct { items []string mu sync.Mutex } func (buff *buffer) add(item string) { buff.mu.Lock() defer buff.mu.Unlock() if len(buff.items)Explaining the implementation
Code link: https://github.com/jcelsocosta/race_condition/blob/main/producerconsumer/buffer/producerconsumer.go
https://www.cin.ufpe.br/~cagf/if677/2015-2/slides/08_Concorrencia (Jorge).pdf
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3