Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apple doc's GCD Producer-Consumer solution wrong?

Tags:

People also ask

How do you solve a consumer producer problem?

Producer consumer problem is a classical synchronization problem. We can solve this problem by using semaphores. A semaphore S is an integer variable that can be accessed only through two standard operations : wait() and signal().

What is Producer-Consumer problem with example?

The producer consumer problem is a synchronization problem. There is a fixed size buffer and the producer produces items and enters them into the buffer. The consumer removes the items from the buffer and consumes them.

Why is the Producer-Consumer problem relevant in operating systems?

The Producer-Consumer problem is used for multi-process synchronization, which means synchronization between more than one processes. In this problem, we have one producer and one consumer. The producer is the one who produces something, and the consumer is the one who consumes something, produced by the producer.

What is the Producer-Consumer problem discuss with algorithm?

The Producer-Consumer problem is a classic synchronization problem in operating systems. The problem is defined as follows: there is a fixed-size buffer and a Producer process, and a Consumer process. The Producer process creates an item and adds it to the shared buffer.


In the Migrating Away from Threads section of Apple's Concurrency Programming Guide, there is
Changing Producer-Consumer Implementations, which claims that the typical multistep pthread mutex + condition variable implementation can be simplified using GCD.

With dispatch queues, you can simplify the producer and consumer implementations into a single call:

dispatch_async(queue, ^{
  // Process a work item.
});

When your producer has work to be done, all it has to do is add that work to a queue and let the queue process the item.

The Producer-Consumer problem is also known as the Bounded-Buffer problem, yet the above makes no mention of a buffer, its bound or the consumer, let alone blocking the producer & consumer in order to avoid over/under runs.

How can this be a valid solution?