在iOS中,Apple提供了很多中加锁来保证线程安全的方法,其中有一个就是@syncnized(){}
。
@syncnized(){}
也可以理解成一个语法糖,接受一个对象做为参数,进行加锁和解锁的操作,来达到线程安全的目的。
通常我们会像下面这样使用它:
@synchronized (self) {
...
doSomething();
...
}
为什么传一个self就可以了呢?锁是怎么和这个self关联上的呢?用self加锁会不会引起其他问题呢?
Using the @synchronized Directive
The @synchronized
directive is a convenient way to create mutex locks on the fly in Objective-C code. The @synchronized
directive does what any other mutex lock would do—it prevents different threads from acquiring the same lock at the same time. In this case, however, you do not have to create the mutex or lock object directly. Instead, you simply use any Objective-C object as a lock token, as shown in the following example:
- (void)myMethod:(id)anObj
{
@synchronized(anObj){
/*
Any code is protected here!
*/
}
}
The object passed to the @synchronized
directive is a unique identifier used to distinguish the protected block. If you execute the preceding method in two different threads, passing a different object for the anObj
parameter on each thread, each would take its lock and continue processing without being blocked by the other. If you pass the same object in both cases, however, one of the threads would acquire the lock first and the other would block until the first thread completed the critical section.
As a precautionary measure, the @synchronized
block implicitly adds an exception handler to the protected code. This handler automatically releases the mutex in the event that an exception is thrown. This means that in order to use the @synchronized
directive, you must also enable Objective-C exception handling in your code. If you do not want the additional overhead caused by the implicit exception handler, you should consider using the lock classes.
For more information about the @synchronized
directive, see The Objective-C Programming Language.
告诉我们 @synchronized
在被保护的代码上暗中添加了一个异常处理。为的是同步某对象时如若抛出异常,锁会被释放掉。
有关@synchronized
的源码在这里可以找到。
其中最为关键的代码就是
// Begin synchronizing on 'obj'.
// Allocates recursive mutex associated with 'obj' if needed.
// Returns OBJC_SYNC_SUCCESS once lock is acquired.
int objc_sync_enter(id obj)
{
int result = OBJC_SYNC_SUCCESS;
if (obj) {
SyncData* data = id2data(obj, ACQUIRE);
assert(data);
data->mutex.lock();
} else {
// @synchronized(nil) does nothing
if (DebugNilSync) {
_objc_inform("NIL SYNC DEBUG: @synchronized(nil); set a breakpoint on objc_sync_nil to debug");
}
objc_sync_nil();
}
return result;
}
// End synchronizing on 'obj'.
// Returns OBJC_SYNC_SUCCESS or OBJC_SYNC_NOT_OWNING_THREAD_ERROR
int objc_sync_exit(id obj)
{
int result = OBJC_SYNC_SUCCESS;
if (obj) {
SyncData* data = id2data(obj, RELEASE);
if (!data) {
result = OBJC_SYNC_NOT_OWNING_THREAD_ERROR;
} else {
bool okay = data->mutex.tryUnlock();
if (!okay) {
result = OBJC_SYNC_NOT_OWNING_THREAD_ERROR;
}
}
} else {
// @synchronized(nil) does nothing
}
return result;
}
这段代码里有 int objc_sync_enter(id obj)
和int objc_sync_exit(id obj)
两个函数。在用@synchronized(){}
的时候,其实上是调用了下面的代码。
@try {
objc_sync_enter(synchronizeTarget);
...
...
...
} @finally {
objc_sync_exit(synchronizeTarget);
}
这两个函数中和锁相关的代码就是data->mutex.lock()
和 data->mutex.tryUnlock()
,这个data就是一个结构体类型,结构如下:
typedef struct SyncData {
struct SyncData* nextData;
DisguisedPtr<objc_object> object;
int32_t threadCount; // number of THREADS using this block
recursive_mutex_t mutex;
} SyncData;
也就是说@synchronized
最终还是用了递归锁recursive_mutex_t
来实现的。
到这里有关@synchronized
大概的流程已经了解了。
如果要对@synchronized
debug,可以关注下这句话:
_objc_inform("NIL SYNC DEBUG: @synchronized(nil); set a breakpoint on objc_sync_nil to debug");
网友评论