[[Property:title|Single-element producer-consumer]] [[Property:weight|-15]] [[Property:uuid|25d3e585-0eb6-efa8-ada9-8ee596df5ada]] {{Beta}} =Description= The single-element producer-consumer is a variant of the classic [http://en.wikipedia.org/wiki/Producer-consumer_problem producer-consumer] problem. A producer produces products, in this case integers, which are consumed by a consumer. The producer and consumer are managed by separate [[Concurrent Eiffel with SCOOP#Processors|processors]], so any access they have to one another must be synchronized through scoop mechanisms. =Highlights= In the single-element producer-consumer only a single producer and single consumer are created, and there is only storage allowing for a single instance of the product. So, effectively in this example, the bounded buffer of the classic producer-consumer problem has a size of one and is not a shared resource. Rather, the single product element (in this case, an integer) is held by the producer and provided to the consumer upon request. The PRODUCER class interface exposes features make_something in which a product is produced and get_something: INTEGER which allows a consumer to retrieve the latest product, if one is available. Feature has_something: BOOLEAN is the indicator of whether an integer has been produced and is available for consumption. The CONSUMER class has a feature take that consumes a product from the producer (by calling {PRODUCER}.get_something), if a product exists. The precondition for take is based on {PRODUCER}.has_something. This is an [[Concurrent Eiffel with SCOOP#Design by Contract and SCOOP|uncontrolled precondition]], so instances of CONSUMER are not obliged to ensure that it holds before calling take. Rather, under SCOOP, this precondition will ensure that the application of take waits until the producer does have a product available for consumption. Likewise, there is an uncontrolled precondition on {PRODUCER}.make_something that allows the feature to be applied only when has_something does not hold. So the heart of the problem is the synchronization between producer and consumer. If there's already a product in storage, the producer cannot produce more and must wait. Only when the consumer consumes the current product can the producer produce again. On the other side, if there's a product currently in storage with the producer, then the consumer can consume that product. Otherwise, the consumer must wait until the producer produces a new product. The example's root class APPLICATION declares a PRODUCER and a CONSUMER, both separate. APPLICATION starts the example running by creating the producer and consumer, then by looping calls to the {PRODUCER}.make_something requesting it constantly to produce products.