Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6743849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:00:42+00:00 2026-05-26T12:00:42+00:00

In the Migrating Away from Threads section of Apple’s Concurrency Programming Guide, there is

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T12:00:43+00:00Added an answer on May 26, 2026 at 12:00 pm

    In the solution decribed at that Apple document:

    1. There is no buffer because no buffer is needed;
    2. System load is the bound;
    3. Consumers are tasks.

    Say you have multiple producers and consumers, producers place data in a shared buffer and consumers read data from that shared buffer. A semaphore or monitor is used to synchronise access to the shared buffer, and the buffer size is fixed so as to limit the amount of data that are being produced according to the rate they’re being consumed, hence throttling the producer.

    Under Grand Central Dispatch, consumers are tasks dispatched to a queue. Since tasks are Objective-C blocks, a producer doesn’t need a buffer to tell a consumer about the data it should process: Objective-C blocks automatically capture objects they reference.

    For example:

    // Producer implementation
    while (…) {
        id dataProducedByTheProducer;
    
        // Produce data and place it in dataProducedByTheProducer
        dataProducedByTheProducer = …;
    
        // Dispatch a new consumer task
        dispatch_async(queue, ^{
            // This task, which is an Objective-C block, is a consumer.
            //
            // Do something with dataProducedByTheProducer, which is
            // the data that would otherwise be placed in the shared
            // buffer of a traditional, semaphore-based producer-consumer
            // implementation.
            //
            // Note that an Objective-C block automatically keeps a
            // strong reference to any Objective-C object referenced
            // inside of it, and the block releases said object when
            // the block itself is released.
    
            NSString *s = [dataProducedByTheProducer …];
        });
    }
    

    The producer may place as many consumer tasks as data it can produce. However, this doesn’t mean that GCD will fire the consumer tasks at the same rate. GCD uses operating system information to control the amount of tasks that are executed according to the current system load. The producer itself isn’t throttled, and in most cases it doesn’t have to be because of GCD’s intrinsic load balancing.

    If there’s actual need to throttle the producer, one solution is to have a master that would dispatch n producer tasks and have each consumer notify the master (via a task that’s dispatched after the consumer has finished its job) that it has ended, in which case the master would dispatch another producer task. Alternatively, the consumer itself could dispatch a producer task upon completion.

    Specifically answering the items you’ve addressed:

    The Producer-Consumer problem is also known as the Bounded-Buffer problem, yet the above makes no mention of a buffer

    A shared buffer isn’t needed because consumers are Objective-C blocks, which automatically capture data that they reference.

    its bound

    GCD bounds the number of dispatched tasks according to the current system load.

    or the consumer

    Consumers are the tasks dispatched to GCD queues.

    let alone blocking the producer & consumer in order to avoid over/under runs

    There’s no need for blocking since there’s no shared buffer. As each consumer is an Objective-C block capturing the produced data via the Objective-C block context capturing mechanism, there’s a one-to-one relation between consumer and data.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're migrating our dev shop away from XCode to MonoTouch. In Objective-C, the [super]
We're migrating from Perforce to GIT. In perforce, there were some files that I'd
I am doing some research on migrating away from Microsoft sql server to postgresql
we are migrating from WebLogic web-services to Spring-WS (1.5.X). There is currently one issue
I might propose migrating away from VSS due to its inability to grant and
I'm using PDO after migrating away from the mysql library. What do I use
Migrating a project from ASP.NET 1.1 to ASP.NET 2.0 and I keep hitting this
After migrating my whole setup from Java 1.5 to 1.6 (J2EE, Tomcat) a couple
Background: Migrating an application from ball of mud to MVC. Many classes contain HTML
I am migrating a struts application from Websphere to Tomcat 6 and my application

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.