concurrent nsoperations execute in a serial fashion?

From: Apple docs on managing concurrency:

NsOperation

Write a custom subclass and override one method: main. The main method gets called to perform the operation when the NSOperationQueue schedules it to run. NSOperation classes written in this way are known as non-concurrent operations, because the developer is not responsible for spawning threads—multi-threading is all handled by the super class. (Don't be confused by the terminology: just because an operation is non-concurrent, does not mean it cannot be executed concurrently, it simply means that you don't have to handle the concurrency yourself.)

I think overriding main is the easiest way to use NSOperation , but the apple site says its non-concurrent does it mean that the nsoperations in the nsoperation queue(when only overriding main) would execute serially?

I don't want to execute my operations serially, but I want to get my operations parallel with as minimum effort as possible.


No, they will not necessarily be run serially. Just as the paragraph you quoted noted, it "does not mean it cannot be executed concurrently".

So what does determine if they can run in parallel? The NSOperationQueue and any dependencies you set up between the ops. A little later in that same document, the section entitled "Running Operations" explains:

  • you can specify (limit) the number of threads the queue will spawn, and
  • "NSOperationQueue will automatically determine how many threads it should spawn."
  • And in the Concurrency Programming Guide it elaborates:

    In most cases, operations are executed shortly after being added to a queue, but the operation queue may delay execution of queued operations for any of several reasons. Specifically, execution may be delayed if queued operations are dependent on other operations that have not yet completed. Execution may also be delayed if the operation queue itself is suspended or is already executing its maximum number of concurrent operations.

    What you do give up by using non-concurrent operations is that the operation itself isn't intended to set up new threads, etc.


    I am not sure about it, but i am using "dispatch_queue_create" to create parallel processes. Suppose if i want to do two functionalities at same time, i am using the following code. Please see if it will help you,

    dispatch_queue_t queue = dispatch_queue_create("FirstOperation", 0ul);
    dispatch_async(queue, ^{
    
        //Do your functionality here
    
        dispatch_sync(dispatch_get_main_queue(), ^{
            //Do your UI updates here
        });
    });
    dispatch_release(queue);
    
    
    dispatch_queue_t queue2 = dispatch_queue_create("SecondOperation", 0ul);
    dispatch_async(queue2, ^{
    
        //Do your functionality here
    
        dispatch_sync(dispatch_get_main_queue(), ^{
            //Do your UI updates here
        });
    });
    dispatch_release(queue2);
    
    链接地址: http://www.djcxy.com/p/65122.html

    上一篇: 在另一个NSOperation中执行NSOperations

    下一篇: 并发的操作系统以串行方式执行?