开发者

Mutex in JNI using Foundation NSLock

I have some objective-c code that uses an NSLock to implement a sort of transaction. The object is locked on a "begin transaction", several other calls are made with the lock in place, and then it's released with a "commit". I'm writing a JNI glue layer to access this code from Java, but the lock is behaving differently in JNI vs pure objc code.

I have unit tests in both Java and objc that exercise the code tha开发者_JAVA技巧t makes the lock. The objc test passes, but in the Java test [anNSLock tryLock] returns false even though [anNSLock lock] hasn't been called.

Is there a recommended way to have a mutex in JNI? I'm not sure what the underlying mechanism for NSLock is.

Thanks!


The docs for NSLock says that NSLock uses Posix threads. Does Java use Posix threads?

A couple of alternative: The first is to create a synchronized wrapper in Java for your object. The second is to use the JNI MonitorEnter and MonitorExit methods for synchronization.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜