iOS GCD custom concurrent queue execution sequence

匆匆过客 提交于 2019-12-01 20:24:34

问题


I have question regarding this issue , According to Apple's documents

Concurrent Concurrent queues (also known as a type of global dispatch queue) execute one or more tasks concurrently, but tasks are still started in the order in which they were added to the queue. The currently executing tasks run on distinct threads that are managed by the dispatch queue. The exact number of tasks executing at any given point is variable and depends on system conditions. In iOS 5 and later, you can create concurrent dispatch queues yourself by specifying DISPATCH_QUEUE_CONCURRENT as the queue type. In addition, there are four predefined global concurrent queues for your application to use. For more information on how to get the global concurrent queues, see Getting the Global Concurrent Dispatch Queues.

And i do a test, using the sample code ,

  dispatch_queue_t concurrentQueue;
    concurrentQueue = dispatch_queue_create("com.gcd.concurrentQueue",
                                                 DISPATCH_QUEUE_CONCURRENT);

    dispatch_async(concurrentQueue, ^{
         NSLog(@"First job ");
    });
    dispatch_async(concurrentQueue, ^{
        NSLog(@"Second job");
    });

    dispatch_async(concurrentQueue, ^{
        NSLog(@"Third job ");
  });

But the results seems not as the order in which they are added, here is the results,

2015-06-03 18:36:38.114 GooglyPuff[58461:1110680] First job 
2015-06-03 18:36:38.114 GooglyPuff[58461:1110682] Third job
2015-06-03 18:36:38.114 GooglyPuff[58461:1110679] Second job

So my question is , shouldn't it be First, Second , Third ?

Any advice is welcome , and thanks for your help.


回答1:


"Concurrent" means they run at the same time and no assumptions should be made about where in their progress any of them will be at any given moment and which will finish first. That is the whole meaning and implication of concurrency: between one line of code and the next in one concurrent operation - even during one line of code - anything else from any other concurrent operation might be happening.

So, in answer to your particular question, these tasks may have started in a known order, but that happened very quickly, and after that point their progress is interleaved unpredictably. And your NSLog calls are part of that progress; they do not, and cannot, tell you when the tasks started!




回答2:


The documentation is correct - they will indeed start in the order you added them to the queue. Once in the queue, they will be started one after the other, but on concurrent threads. The order they will finish is dependent on how long the task will take to execute. Here's a thought experiment, imagine your code was like this instead:

    dispatch_async(concurrentQueue, ^{
         JobThatTakes_3_SecToExecute(); // Job 1 (3 seconds to execute)
    });

    dispatch_async(concurrentQueue, ^{
        JobThatTakes_2_SecToExecute(); // Job 2 (2 seconds to execute)
    });

    dispatch_async(concurrentQueue, ^{
        JobThatTakes_1_SecToExecute(); // Job 3 (1 second to execute)
    });

The overhead in and out of the queue should be very small compared to these job lengths, so you would expect them to finish up in about the time that their task takes to execute. In this case they'd finish roughly 1 second apart starting with Job 3, then 2, then 1. The total time the queue would take to complete will be about the length of Job 1, since it takes the longest to execute. This is lovely, since the total time is set primarily by the longest job, not the sum of the jobs. However, you don't have any say in what order they finish, since that's dictated by the task duration.

Change dispatch_async to dispatch_sync in this example and the queue will take about 6 seconds to complete. They'll come out in this order: Job 1, 2, then 3. This will guarantee that your results come out in the order you wanted, but it will take much longer.

So back to the significance of what the docs mean by "tasks are still started in the order in which they were added to the queue" for concurrent queues. This will be noticeable if your job is resource constrained. Say you're putting a big pile of long duration tasks in a concurrent queue on a 2 CPU machine. It is unlikely you'll be able to run a dozen CPU-pegging tasks concurrently here; some will have to wait while others run. The order that you put them into the queue will decide who gets to run next as resources free up. In your example, the tasks are of super short duration and involve console locking (as Rob mentioned), so queue / locking overhead can mess with your expectations.

Another (probably more important) reason the order of execution in concurrent queues matter is when barriers are used. You may need to run some sort of a task every N other tasks, which is where a barrier would come in handy. The fixed order of execution will assure that the barrier executes after N tasks have completed concurrently, provided you put the barrier in the queue in the right spot.



来源:https://stackoverflow.com/questions/30633579/ios-gcd-custom-concurrent-queue-execution-sequence

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!