Dispatch queues: How to tell if they're running and how to stop them

前端 未结 3 1216
北荒
北荒 2020-11-28 20:54

I\'m just playing around with GCD and I\'ve written a toy CoinFlipper app.

Here\'s the method that flips the coins:

- (void)flipCoins:(NSUInteger)nFl         


        
3条回答
  •  爱一瞬间的悲伤
    2020-11-28 21:30

    If you have a serial dispatch queue OR a concurrent dispatch queue, here is a code that can do the same thing.

    BOOL __block queueIsEmpty = false;
    dispatch_barrier_async (_dispatchQueue, ^{
        queueIsEmpty = true;
    });
    
    while (!queueIsEmpty) {
        int i = 0;  // NOOP instruction
    }
    
    // At this point your queue should be empty.
    

提交回复
热议问题