如何检查对象是否正在@synchronized [英] How do I check if an object is being @synchronized

查看:129
本文介绍了如何检查对象是否正在@synchronized的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时,我编写了以下代码来同步例程:

Sometimes I wrote the following code to synchronized a routine:

@synchronized(objToBeSync){ .... }

当两个线程尝试同时访问同步块时,一个线程将阻塞其他线程,直到一个线程退出同步块.

When two threads try to access the sync block at the same time, one will block the others, until the one exits the sync block.

但是,有时我不希望一个块阻塞另一个对象,但是其他对象则检查对象是否正在同步,然后再执行其他操作,所以我必须做这样的事情:

However, sometimes I don't want one blocks the other, but the others check if the object is being synchronized, and then do some other thing so I have to do sth like this:

@synchronized(objToBeSync){
    _isBeingSync = YES;
    ... 
    _isBeingSync = NO;
}

_isBeingSync是检查objToBeSync是否正在同步的附加变量.其他线程在继续工作之前会检查_isBeingSync.我的问题是objc是否提供了某种东西来直接检查objToBeSync,但没有引入额外的var来标记其状态.

_isBeingSync is an additional var on checking if objToBeSync is being sync. The other threads check _isBeingSync before they continue their work. And my question is that does objc provide sth to check objToBeSync directly but not introduce an additional var to mark down its status.

推荐答案

编译器将@synchronized(objToBeSync) { ... }转换为

callq   _objc_sync_enter
...
callq   _objc_sync_exit

并从 Objective-C运行时源代码(objc-sync.mm, objc-os.mm,objc-lockdebug.mm,objc-os.h),可以看到这些功能主要是做一个

and from the Objective-C Runtime Source Code (objc-sync.mm, objc-os.mm, objc-lockdebug.mm, objc-os.h) one can see that these functions mainly do a

pthread_mutex_lock(m->mutex);
...
pthread_mutex_unlock(m->mutex);

其中,m->mutex是具有PTHREAD_MUTEX_RECURSIVE属性的pthread_mutex_t 使用运行时内部的缓存与对象objToBeSync关联.

where m->mutex is a pthread_mutex_t with the PTHREAD_MUTEX_RECURSIVE attribute that is associated with the object objToBeSync, using a cache internal to the runtime.

因此,您的问题的直接答案是:不,没有公共API可以获取 对象的锁定"状态以及访问内部互斥锁对我来说几乎是不可能的.

So the direct answer to your question is: No, there is no public API to get the "locked" status of an object, and accessing the internal mutex seems almost impossible to me.

因此,如果您有此要求,则应使用其他锁定机制, 例如Posix Mutex锁NSLockNSRecursiveLock.所有这些锁都有一个 可用于获取锁的尝试"方法,或立即失败而不会阻塞.

Therefore, you should use a different locking mechanism if you have that requirement, e.g. a Posix Mutex Lock, NSLock or NSRecursiveLock. All these locks have a "try" method that can be used to aquire the lock, or fail immediately without blocking.

请参阅 线程编程指南:同步" 进行概述.

请注意,与其他锁定机制相比,@synchronized隐式向该块添加了异常处理程序,以便 如果抛出异常,则互斥体被释放.

Note that @synchronized (in contrast to the other locking mechanisms) implicitly adds an exception handler to the block so that the mutex is released if an exception is thrown.

@synchronized递归锁,即相同线程可以输入受保护的 代码没有阻塞.如果这与您的代码有关,则必须使用 NSRecursiveLock或具有递归"属性的Posix互斥锁.

Also @synchronized is a recursive lock, i.e. the same thread can enter the protected code without blocking. If that is relevant to your code, you would have to use NSRecursiveLock or a Posix Mutex Lock with the "recursive" attribute.

请注意,为此目的使用简单的实例变量_isBeingSync 竞争条件,将无法安全工作.

Note that using a simple instance variable _isBeingSync for this purpose is subject to race conditions and will not work safely.

这篇关于如何检查对象是否正在@synchronized的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆