使用dispatch_sync作为互斥锁

Mr.*_*ous 5 multithreading objective-c critical-section grand-central-dispatch ios

这是我需要做的.我希望dispatch_sync是使用GCD完成它的最佳方式

我有一段临界区代码放在Appdelegate的applicationDidBecomeActive回调中.

我在dispatch_sync调用中包装该方法,因此无论调用applicationDidBecomeActive多少次,它都只被调用一次

- (void)applicationDidBecomeActive:(UIApplication *)application{    
dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{    
    NSLog(@"Thread created");
    //crtical code
    [self runCriticalSection];        
});}
Run Code Online (Sandbox Code Playgroud)

这是使用dispatch_sync执行此操作的正确方法吗?

Mar*_*n R 13

dispatch_sync()在块完成之前不会返回,这意味着 在完成执行applicationDidBecomeActive之前不会返回runCriticalSection.

这可能不是你想要的,因此你必须使用dispatch_async()(如在另一个答案中已经说明的那样).

但是runCriticalSection如果前一个仍然在运行,你不希望另一个启动.这可以通过"计数信号量"(这也是GCD的一个特征)来实现:

static dispatch_semaphore_t sema; // The semaphore
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
    // Initialize with count=1 (this is executed only once):
    sema = dispatch_semaphore_create(1);
});

// Try to decrement the semaphore. This succeeds if the count is still 1
// (meaning that runCriticalSection is not executing), and fails if the 
// current count is 0 (meaning that runCriticalSection is executing):
if (dispatch_semaphore_wait(sema, DISPATCH_TIME_NOW) == 0) {
    // Success, semaphore count is now 0.
    // Start asynchronous operation.
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        //critical code
        [self runCriticalSection];
        // Increment the semaphore count (from 0 to 1), so that the next call
        // to applicationDidBecomeActive will start a new operation:
        dispatch_semaphore_signal(sema);
    });
}
Run Code Online (Sandbox Code Playgroud)