What advantage(s) does dispatch_sync have over @synchronized?

前端 未结 4 1443
萌比男神i
萌比男神i 2020-12-02 08:22

Lets say I want to make this code thread-safe:

- (void) addThing:(id)thing { // Can be called from different threads
    [_myArray addObject:thing];
}
         


        
4条回答
  •  囚心锁ツ
    2020-12-02 08:39

    I have found that dispatch_sync() is a poor way to do locking, it does not support nested calls.

    So you can't call dispatch_sync on a serial Q and then call it again in a subroutine with the same Q. Which means it does not behave in the same way as @synchronized does at all.

提交回复
热议问题