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 9124949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:41:12+00:00 2026-06-17T06:41:12+00:00

How to control and balance the number of threads my app is executing, how

  • 0

How to control and balance the number of threads my app is executing, how to limit their number to avoid app’s blocking because thread limit is reached?

Here on SO I saw the following possible answer: “Main concurrent queue (dispatch_get_global_queue) manages the number of threads automatically” which I don’t like for the following reason:

Consider the following pattern (in my real app there are both more simple and more complex examples):

dispatch_queue_t defaultBackgroundQueue() {
    return dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
}

dispatch_queue_t databaseQueue() {
    dispatch_queue_create("Database private queue", 0);
}

dispatch_async(defaultBackgroundQueue(), ^{
    [AFNetworkingAsynchronousRequestWithCompletionHandler:^(data){
        dispatch_async(databaseQueue(), ^{
            // data is about 100-200 elements to parse
            for (el in data) {

            }

            maybe more AFNetworking requests and/or processing in other queues or

            dispatch_async(dispatch_get_main_queue(), ^{
                // At last! We can do something on UI.
            });
        });
    }];
});

This design very often leads to the situation when:

  1. The app is locked because of threads limit is reached (something like > 64)
  2. the slower and thus narrow queues can be overwhelmed with a large number of pending jobs.
  3. the second one also can produce a cancellation problem – if we have 100 jobs already waiting for execution in a serial queue we can’t cancel them at once.

The obvious and dumb solution would be to replace sensitive dispatch_async methods with dispatch_sync, but it is definitely the one I don’t like.

What is recommended approach for this kind of situations?

I hope an answer more smart than just “Use NSOperationQueue – it can limit the number of concurrent operations” does exist (similar topic: Number of threads with NSOperationQueueDefaultMaxConcurrentOperationCount).

UPDATE 1: The only decent pattern is see: is to replace all dispatch_async’s of blocks to concurrent queues with running these blocks wrapped in NSOperations in NSOperationQueue-based concurrent queues with max operations limit set (in my case maybe also set a max operations limit on the NSOperationQueue-based queue that AFNetworking run all its operations in).

  • 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-06-17T06:41:14+00:00Added an answer on June 17, 2026 at 6:41 am

    You are starting too many network requests. AFAIK it’s not documented anywhere, but you can run up to 6 simultaneous network connections (which is a sensible number considering RFC 2616 8.1.4, paragraph 6). After that you get locking, and GCD compensates creating more threads, which by the way, have a stack space of 512KB each with pages allocated on demand. So yes, use NSOperation for this. I use it to queue network requests, increase the priority when the same object is requested again, pause and serialize to disk if the user leaves. I also monitor the speed of the network requests in bytes/time and change the number of concurrent operations.

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

Sidebar

Related Questions

DockingManager control has been removed from their suite in Q1 2011, so now I
Here's the issue. I'm using ASP.NET Forms Authentication for an account balance type website.
Control.Invoke executes the specified delegate on the thread that owns the control's underlying window
I am looking for a way to control the balance of a sound (left-right)
Control Panel -> Security Center I really like the components/controls which are used to
foreach (Control c in panPrev.Controls) { if (c.Tag == move) works well, but produce
I have a machine control system in Python that currently looks roughly like this
I have an Image control in my asp.net page. I gave an image url
I have a custom control which displays results of some operations. It is hidden
How do I version control my map/reduce functions for CouchDB? I'd like to be

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.