using dispatch_sync in Grand Central Dispatch

前端 未结 8 2089
孤城傲影
孤城傲影 2020-11-30 17:25

Can anyone explain with really clear use cases what the purpose of dispatch_sync in GCD is for? I can\'t understand where and why I would have to u

8条回答
  •  北海茫月
    2020-11-30 17:45

    dispatch_sync is semantically equivalent to a traditional mutex lock.

    dispatch_sync(queue, ^{
        //access shared resource
    });
    

    works the same as

    pthread_mutex_lock(&lock);
    //access shared resource
    pthread_mutex_unlock(&lock);
    

提交回复
热议问题