dispatch_async和dispatch_sync在Grand Central Dispatch中使用串行队列

时间:2020-12-28 09:00:49

OK, I love Grand Central Dispatch and after using it with relative success but this is something I don't fully understand.

好的,我喜欢大*分派,在相对成功地使用它之后但这是我不完全理解的。

Suppose I have created my own serial queue using

假设我已经创建了自己的串行队列。

dispatch_queue_t myQueue;
myQueue = dispatch_queue_create("myQueue", NULL);

After that I do this:

之后我这样做:

dispatch_async(myQueue, ^{
  [self doStuff1];
});

// and a few lines later...

dispatch_sync(myQueue, ^{
  [self doStuff2];
});

The first dispatch is async. So, it will be done concurrently, right? How can that be if myQueue is serial? How can a serial queue do things in parallel or, if you will, out of order?

第一个分派是异步的。所以,它会同时进行,对吧?如果myQueue是串行的,那怎么可能呢?一个串行队列如何并行地做事情,或者,如果你愿意,是无序的?

thanks

谢谢

2 个解决方案

#1


57  

dispatch_async() means that the block is enqueued and dispatch_async()returns to enqueueing another task/block (possibly) prior to the block being executed.

dispatch_async()表示块被放入队列,dispatch_async()返回到在正在执行的块之前对另一个任务/块进行排队。

With dispatch_sync(), the block is enqueued and the function will not continue enqueueing another task/block until the block is executed.

使用dispatch_sync(),块被加入队列,函数在执行块之前不会继续对另一个任务/块进行排队。

The blocks are still executed serially. You could execute 100 dispatch_async() calls, each with a block that sleeps for 100 seconds, and it'd be really fast. Follow that with a call to dispatch_sync() on the same serial queue and dispatch_sync() will return ~10,000 seconds later.

块仍然是串行执行的。您可以执行100个dispatch_async()调用,每个调用都有一个可以休眠100秒的块,而且速度非常快。接下来,对同一个串行队列上的dispatch_sync()进行调用,然后dispatch_sync()将在大约10,000秒后返回。


To put it more simply:

更简单地说:

dispatch_async(serialQ, block1);
dispatch_async(serialQ, block2);
dispatch_sync(serialQ, block3);

block1 will be executed before block2 which will be executed before block3. That is the order guaranteed by the serial queue.

block1将在block2之前被执行,在block3之前被执行。这是串行队列保证的顺序。

However, the calls to dispatch_async() may return before any of the blocks start executing. The dispatch_sync() will not return before all three blocks are executed!

但是,对dispatch_async()的调用可能会在任何块开始执行之前返回。在执行所有三个块之前,dispatch_sync()不会返回!

#2


3  

Neither dispatch_async or dispatch_sync change the way the block gets queued. If the queue is serial, the blocks will execute in a serial manner, if the queue is concurrent, in a concurrent manner.

dispatch_async和dispatch_sync都不会改变块排队的方式。如果队列是串行的,那么块将以串行方式执行,如果队列是并发的,则以并发方式执行。

The important difference between the two is that dispatch_sync queues the block and waits on the current execution thread until that block is executed and dispatch_async just queues the block and continues the execution of the subsequent instructions.

两者之间的重要区别是,dispatch_sync对块进行队列,并在当前执行线程上等待,直到该块被执行,dispatch_async只是对该块进行队列,并继续执行后续指令。

#1


57  

dispatch_async() means that the block is enqueued and dispatch_async()returns to enqueueing another task/block (possibly) prior to the block being executed.

dispatch_async()表示块被放入队列,dispatch_async()返回到在正在执行的块之前对另一个任务/块进行排队。

With dispatch_sync(), the block is enqueued and the function will not continue enqueueing another task/block until the block is executed.

使用dispatch_sync(),块被加入队列,函数在执行块之前不会继续对另一个任务/块进行排队。

The blocks are still executed serially. You could execute 100 dispatch_async() calls, each with a block that sleeps for 100 seconds, and it'd be really fast. Follow that with a call to dispatch_sync() on the same serial queue and dispatch_sync() will return ~10,000 seconds later.

块仍然是串行执行的。您可以执行100个dispatch_async()调用,每个调用都有一个可以休眠100秒的块,而且速度非常快。接下来,对同一个串行队列上的dispatch_sync()进行调用,然后dispatch_sync()将在大约10,000秒后返回。


To put it more simply:

更简单地说:

dispatch_async(serialQ, block1);
dispatch_async(serialQ, block2);
dispatch_sync(serialQ, block3);

block1 will be executed before block2 which will be executed before block3. That is the order guaranteed by the serial queue.

block1将在block2之前被执行,在block3之前被执行。这是串行队列保证的顺序。

However, the calls to dispatch_async() may return before any of the blocks start executing. The dispatch_sync() will not return before all three blocks are executed!

但是,对dispatch_async()的调用可能会在任何块开始执行之前返回。在执行所有三个块之前,dispatch_sync()不会返回!

#2


3  

Neither dispatch_async or dispatch_sync change the way the block gets queued. If the queue is serial, the blocks will execute in a serial manner, if the queue is concurrent, in a concurrent manner.

dispatch_async和dispatch_sync都不会改变块排队的方式。如果队列是串行的,那么块将以串行方式执行,如果队列是并发的,则以并发方式执行。

The important difference between the two is that dispatch_sync queues the block and waits on the current execution thread until that block is executed and dispatch_async just queues the block and continues the execution of the subsequent instructions.

两者之间的重要区别是,dispatch_sync对块进行队列,并在当前执行线程上等待,直到该块被执行,dispatch_async只是对该块进行队列,并继续执行后续指令。