How do I use NSConditionLock? Or NSCondition(如何使用 NSConditionLock?或 NSCondition)
问题描述
I am try to make one function wait for another, and I would like to use NSCondionLock in order to accomplish this. I am not asking for help, but really hoping someone could show me a decent tutorial or example to explain NSConditionLock, or possibly suggest a better method.
EDIT: as @Bonshington commented, this answer refers to NSCondition (as opposed to NSConditionLock):
- (void) method1 {
[myCondition lock];
while (!someCheckIsTrue)
[myCondition wait];
// Do something.
[myCondition unlock];
}
- (void) method2 {
[myCondition lock];
// Do something.
someCheckIsTrue = YES;
[myCondition signal];
[myCondition unlock];
}
The someCheckIsTrue can be anything, it could be a simple BOOL variable or even something like [myArray count] == 0 && color == kColorRed, it doesn't matter. It only matters that in one method you check for a condition while you have the lock and in another method you make changes that can make the condition become true also while having the lock. The magic is the wait and signal part: the wait actually unlocks the lock and reacquires it after some other thread called signal.
这篇关于如何使用 NSConditionLock?或 NSCondition的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 NSConditionLock?或 NSCondition
基础教程推荐
- 如何将多个组件添加到 PickerView? 2022-01-01
- 在 iOS8 中无法获得正确的键盘高度值 2022-01-01
- Android:STATE_SELECTED不工作 2022-01-01
- 新的@SystemApi 注解是什么意思,和@hide 有什么区别 2022-01-01
- 可可/目标C(OSX不是iOS)从子对象访问父对象 2022-01-01
- 在 appComponent dagger 2 中动态添加测试模块? 2022-01-01
- - (CGRect)convertRect:(CGRect)rect toView:(UIView *)view 如何工作 2022-01-01
- 我的 UIImageView 的任务 2022-01-01
- 在 Android 模拟器中激活网络位置提供程序? 2022-01-01
- 突出显示朗读文本(在 iPhone 的故事书类型应用程序中) 2022-01-01
