site_archiver@lists.apple.com Delivered-To: cocoa-dev@lists.apple.com Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quevivadev.com; s=default; t=1660837643; bh=mhr6RxGjDGyG3kKfitxbS1Nji9w018CBCyEtZYH5TtM=; h=From:Subject:Date:References:To:In-Reply-To; b=VlKXQBIaVo1ZvHRaY5Y1wXdsUZXJ+/ueaOcAJeYP6/l/IxHbBW5Z54s7aNqRx7iMH So3Z/Z0+0SK1S8MoGeCHgYst//rqJcVhQUjoHkYg8QrY5MyT/fgXZxO1/MhZreObLx +XbsF8C09yiyKg4Kl9EBW0VjMKJBcBZgIaPhrKC4= On Aug 18, 2022, at 7:47 AM, Mike Abdullah <mabdullah@karelia.com> wrote:
It’s not a very good fit, but when you say a “GCD concurrent queue”, you’d need to be more specific. There are several configs possible. Do you mean a global queue, or one you made yourself? If you made it yourself, how did you configure it?
Like Alex, for me this was 10-12 years ago so I don’t remember much about exactly how I tried to use GCD. It spun up too many threads, I re-read the docs and assumed it was spinning up new threads based on file IO blocking, and killing itself. I didn’t see a handy way to limit GCD concurrent queueing, and NSOperationQueue had maxConcurrentOperationCount documented. NSOperationQueue was also more idiomatic Objective-C and easier to use/understand in an Obj-C program.
The tricky problem is that GCD aims to optimise CPU usage. Reading image metadata from disk is almost certainly not CPU bound; it’s going to be limited by disk speed and the file system instead. GCD will have a tendency to see tasks blocked on doing this, not occupying the CPU, and so spin up another task in the hope that one _will_ then use the CPU, eventually grinding to a halt as a huge number of threads contend for access to the file system.
In my case the app reads hundreds images from disk or network file share, performs operations (scale/rotate/crop/etc), rendering on CPU (better accuracy for printing) and writing to disk or network share. Perfect fit for NSOperationQueue with maxConcurrentOperationCount, so I was grateful someone at Apple had already done the hard work and made concurrent programming much easier for people with tasks like mine. The OP is just grabbing image metadata but would almost certainly run into the same problem using GCD's dispatch_async so they’ll also be much better off using NSOperationQueue with maxConcurrentOperationCount. Jim Crate
On 17 Aug 2022, at 20:32, James Crate via Cocoa-dev <cocoa-dev@lists.apple.com> wrote:
I have an app that does some image processing, and when I tried to use GCD it created several hundred threads which didn’t work very well. NSOperationQueue allows you to set the max concurrent operations, and the batch exporting process fully utilizes all logical cores on the CPU.
opsQueue.maxConcurrentOperationCount = NSProcessInfo.processInfo.processorCount;
Maybe I was using GCD wrong, or maybe reading, processing, and writing several hundred images is not a good fit for GCD concurrent queue? In any case NSOperationQueue is easy to use and works well.
Jim Crate
_______________________________________________ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/site_archiver%40lists.appl... This email sent to site_archiver@lists.apple.com