Objective c中的锁 NSLock,NSConditionLock,NSRecursiveLock

对lock 相关的 开发文档做一个简单的翻译。

NSLock

NSLock对象用来协调相同进程中的多个线程。NSLock用来保证临界资源的原子性操作和临界区的原子性执行。

概述:

这文档不是最终版本,未来可能会变动,根据此文档设计的 代码实现 需要详细的测试,文档的新版本可能在未来跟随新的api一起发布。

警告:

NSLock 使用 POSIX threads来实现的。unlock 操作必须与lock 操作使用相同的线程,否则会出现未知错误。 

NSLock 不能当做 循环锁(也有称作递归锁或嵌套锁的,主要特点是允许相同线程 多次上锁,并通过多次 unlock来解锁)来使用 ,

同一线程在 lock之后 未unlock 之前 再次 lock 会造成 永久性死锁。

循环锁,请使用 NSRecursiveLock 。

对没有  lock 的 NSLock对象 进行 unlock 属于编程错误,需要更正,当这种错误发生时,错误提示会被输出到控制台。


An NSLock object is used to coordinate the operation of multiple threads of execution within the same application. AnNSLock object can be used to mediate access to an application’s global data or to protect a critical section of code, allowing it to run atomically.


Overview

Important:Important

This is a preliminary document for an API or technology in development. Although this document has been reviewed for technical accuracy, it is not final. This Apple confidential information is for use only by registered members of the applicable Apple Developer program. Apple is supplying this confidential information to help you plan for the adoption of the technologies and programming interfaces described herein. This information is subject to change, and software implemented according to this document should be tested with final operating system software and final documentation. Newer versions of this document may be provided with future seeds of the API or technology.

Warning:Warning

The NSLock class uses POSIX threads to implement its locking behavior. When sending an unlock message to anNSLock object, you must be sure that message is sent from the same thread that sent the initial lock message. Unlocking a lock from a different thread can result in undefined behavior.

You should not use this class to implement a recursive lock. Calling thelock method twice on the same thread will lock up your thread permanently. Use theNSRecursiveLock class to implement recursive locks instead.

Unlocking a lock that is not locked is considered a programmer error and should be fixed in your code. TheNSLock class reports such errors by printing an error message to the console when they occur.


NSConditionLock

NSConditionLock 类定义了 和特定用户定义的条件联系起来的锁 。使用 NSConditionLock 对象 可以确保  一个线程可以在有在特定条件下才可以 获得锁。一但获得了锁并且执行了临界区代码,这线程可以放弃这个锁 并设置这个相关的条件到一个新的值。 这个条件本身可以是任意值,你可以根据需要设置它。

概述:

这文档不是最终版本,未来可能会变动,根据此文档设计的 代码实现 需要详细的测试,文档的新版本可能在未来跟随新的api一起发布。


NSConditionLock

The NSConditionLock class defines objects whose locks can be associated with specific, user-defined conditions. Using anNSConditionLock object, you can ensure that a thread can acquire a lock only if a certain condition is met. Once it has acquired the lock and executed the critical section of code, the thread can relinquish the lock and set the associated condition to something new. The conditions themselves are arbitrary: you define them as needed for your application.


Overview

Important:Important

This is a preliminary document for an API or technology in development. Although this document has been reviewed for technical accuracy, it is not final. This Apple confidential information is for use only by registered members of the applicable Apple Developer program. Apple is supplying this confidential information to help you plan for the adoption of the technologies and programming interfaces described herein. This information is subject to change, and software implemented according to this document should be tested with final operating system software and final documentation. Newer versions of this document may be provided with future seeds of the API or technology.



NSRecursiveLock

NSRecursiveLock定义了一个可以被同一线程获得多次而不会造成死锁的 lock , 当前线程 无论是 单次上锁还是多次上锁,所有的其他线程都不能访问被锁保护的 区域。

概述:

这文档不是最终版本,未来可能会变动,根据此文档设计的 代码实现 需要详细的测试,文档的新版本可能在未来跟随新的api一起发布。


NSRecursiveLock

NSRecursiveLock defines a lock that may be acquired multiple times by the same thread without causing a deadlock, a situation where a thread is permanently blocked waiting for itself to relinquish a lock. While the locking thread has one or more locks, all other threads are prevented from accessing the code protected by the lock.


Overview

Important:Important

This is a preliminary document for an API or technology in development. Although this document has been reviewed for technical accuracy, it is not final. This Apple confidential information is for use only by registered members of the applicable Apple Developer program. Apple is supplying this confidential information to help you plan for the adoption of the technologies and programming interfaces described herein. This information is subject to change, and software implemented according to this document should be tested with final operating system software and final documentation. Newer versions of this document may be provided with future seeds of the API or technology.




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值