You are comparing a global lock (one lock for all instances) with a recursive lock at the object level (one lock per instance, which can be obtained several times from the same thread). They, as a rule, are not interchangeable - they act and are very different from each other.
The good news: you can use pthread_mutex_t
as a recursive lock that is unique to each instance to achieve the same degree of protection as @synchronized
. Using pthread_mutex_t
also makes blocking acquisitions much, much faster.
To achieve the same effect as @synchronized
using the pthread mutex, declare pthread_mutex_t gLock
as an instance variable, then initialize it as a recursive mutex in -init
. Finally, destroy the mutexes in -dealloc
.
Of course, sub- and base classes may need access to this lock if they rely on @synchronized
semantics to do the right thing through a hierarchy of objects.
@synchronized
slower than veeeeeeery compared to the recursive pthread mutex (the last time I checked).
source share