问题
I'm developing a sort of fast image scan application.
In the -captureOutput:didOutputSampleBuffer:fromConnection:
method I pick up the CVPixelBuffer and add them into an NSOperation
subclass. The NSOperation
takes the buffer and transform it into an image saved on the filesystem.
The -isConcurrent
method of NSOperation
returns YES. After the operation is created, is added to an NSOperationQueue
.
Everything runs fine except for one issue that is affecting the scan frame rate.
Using time profiler I've found that some of the NSOperation are run on the same thread of the AVCaputureVideoOutput delegate that I've created:
dispatch_queue_t queue = dispatch_queue_create("it.CloudInTouchLabs.avsession", DISPATCH_QUEUE_SERIAL);
[dataOutput setSampleBufferDelegate:(id)self queue:queue];
When an operation is running on the same thread of the AV session queue, it affects the frame rate, it happens only with some them, probably GCD is deciding under the hood dispatching on active threads.
The only method I've found to solve that issue, is to create a separate thread and pass it to the the single operations forcing them to run on it.
Is there another way, to force operations run on a different thread?
[EDIT]
Following the @impcc suggestions I made some test.
Results are pretty interesting even if sort of inconsistent.
Test platform is an iPhone 5 connected in debugging mode through an MBP with 16gb of RAM quad core i7. The session has a 60fps output tested with the algorithm in RosyWriter apple sample code.
AVSession queue sync targeted to high priority queue
26 fps where sometimes the thread of the queue is shared with the one of the operation. The time taken inside the delegate methods has an average of 0.02s
14 fps with a thread created just for the operations, if the main method is not called on this thread, the start will be forced to perform on that specific thread. This thread is create one time and kept alive with a dummy port.. The time taken inside the delegate is 0.008.
AVSession queue concurrent targeted to high priority queue
13.5 fps where sometimes the thread of the queue is shared with the one of the operation. The time taken inside the delegate methods has an average of 0.02s, substacially equal to the counterpart with sync queue.
14 fps with a thread created just for the operations, if the main method is not called on this thread, the start will be forced to perform on that specific thread. This thread is create one time and kept alive with a dummy port. The time taken inside the delegate is 0.008.
Conclusion
Concurrent or serial queue doesn't seems to make a big difference, however concurrent for me is not ok cause of the timestamps that I need to take to preserve the sequence of single picture. The fact that is surprising me is the frame drop using an ad-hoc thread, even if the time taken inside the delegate method is considerably less, the frame rate drops about 10fps. Just trusting in GCD the frame rate is better but the delegate methods takes more than 2 times to finish, this probably due to the fact that sometimes the AV queue is used by also the nsoperations.
I can't really get why the time taken inside the delegate doesn't seems to be correlated to the fps. Shouldn't be the faster the better?
By further investigations it really seems that what is stealing time si in the process of adding and probably executing operation in queue.
回答1:
I think you might be misunderstanding the meaning of isConcurrent
. This is totally understandable, since it's incredibly poorly named. When you return YES
from -isConcurrent
what it really means is "I will handle any concurrency needs associated with this operation, and will otherwise behave asynchronously." In this situation, NSOperationQueue
is free to call -start
on your operation synchronously on the thread from which you add the operation. The NSOperationQueue
is expecting that, because you've declared that you will manage your own concurrency, that your -start
method will simply kick off an asynchronous process and return right away. I suspect this is the source of your problem.
If you implemented your operation by overriding -main
then you almost certainly want to be returning NO
from isConcurrent
. To make matters more complicated, the behaviors associated with isConcurrent
have changed over the years (but all that is covered in the official docs.) A fantastic explanation of how to properly implement a returns-YES
-from-isConcurrent
NSOperation
can be found here.
The way I'm reading your question here, it doesn't sound like your NSOperation
subclass actually needs to "manage its own concurrency", but rather that you simply want it to execute asynchronously, "on a background thread", potentially "concurrently" with other operations.
In terms of assuring the best performance for your AVCaptureVideoDataOutputSampleBufferDelegate
callbacks, I would suggest making the queue you pass in to -setSampleBufferDelegate:queue:
be (itself) concurrent and that it target the high-priority global concurrent queue, like this:
dispatch_queue_t queue = dispatch_queue_create("it.CloudInTouchLabs.avsession", DISPATCH_QUEUE_CONCURRENT);
dispatch_set_target_queue(queue, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0));
[dataOutput setSampleBufferDelegate:(id)self queue:queue];
Then you should make the delegate callback methods as lightweight as possible -- nothing more than packaging up the information you need to make the NSOperation
and add it to the NSOperationQueue
.
This should ensure that the callbacks always take precedence over the NSOperations
. (It's my understanding that NSOperationQueue
targets either the main queue (for the NSOperationQueue
that's associated with the main thread and run loop) or the default priority background queue.) This should allow your callbacks to fully keep up with the frame rate.
Another important thing to take away here (that another commenter was getting at) is that if you're doing all your concurrency using GCD, then there is only one special thread -- the main thread. Other than that, threads are just a generic resource that GCD can (and will) use interchangeably with one another. The fact that a thread with a thread ID of X was used at one point to service your delegate callback, and at another point was used to do your processing is not, in and of itself, indicative of a problem.
Hope this helps!
来源:https://stackoverflow.com/questions/20738563/nsoperationqueue-concurrent-operation-and-thread