GCD dispatch concurrent queue freeze with 'Dispatch Thread Soft Limit Reached: 64' in crash log

后端 未结 1 680
逝去的感伤
逝去的感伤 2021-01-12 15:57

My program is a server which handles incoming requests. Each valid request is wrapped in NSOperation and passed to a normal NSOperationQueue.

1条回答
  •  悲&欢浪女
    2021-01-12 16:19

    I can think of 2 possible ways to avoid this problem. (which I'm going to test them and will update later)

    1. Use dispatch_semaphore to limit submitting the block to this concurrent queue.
    2. Limit maxConcurrentOperationCount of the NSOperationQueue.

    Yes, those are two common patterns. For the sake of future readers, the other solution to this “exhausting worker threads” problem is Objective-C's dispatch_apply, also known as concurrentPerform in Swift, which allows concurrent operations in a manner that won’t exhaust your pool of worker threads. But that’s really only applicable when launching a whole series of tasks up front (e.g. you want to parallelize a for loop), not the scenario you outline in your question. But, still, for the record, dispatch_apply/concurrentPerform is the third common solution for this general problem.

    I cannot find information about this limit.

    This used to be covered really nicely in WWDC 2012 video Asynchronous Design Patterns with Blocks, GCD, and XPC, but that video is no longer available (other WWDC 2012 videos are, but not that one, curiously). But they do walk through the limited worker thread issue in WWDC 2015 video Building Responsive and Efficient Apps with GCD.

    0 讨论(0)
提交回复
热议问题