美文网首页移动开发
iOS锁-NSCondition

iOS锁-NSCondition

作者: 口子窖 | 来源:发表于2018-07-11 18:00 被阅读3次

NSCondition

下面是苹果官方文档的说法:

A condition variable whose semantics follow those used for POSIX-style conditions.

Overview

A condition object acts as both a lock and a checkpoint in a given thread. The lock protects your code while it tests the condition and performs the task triggered by the condition. The checkpoint behavior requires that the condition be true before the thread proceeds with its task. While the condition is not true, the thread blocks. It remains blocked until another thread signals the condition object.

The semantics for using an NSCondition object are as follows:

  1. Lock the condition object.
  2. Test a boolean predicate. (This predicate is a boolean flag or other variable in your code that indicates whether it is safe to perform the task protected by the condition.)
  3. If the boolean predicate is false, call the condition object’s wait or waitUntilDate:method to block the thread. Upon returning from these methods, go to step 2 to retest your boolean predicate. (Continue waiting and retesting the predicate until it is true.)
  4. If the boolean predicate is true, perform the task.
  5. Optionally update any predicates (or signal any conditions) affected by your task.
  6. When your task is done, unlock the condition object.

The pseudocode for performing the preceding steps would therefore look something like the following:

lock the condition
while (!(boolean_predicate)) {
    wait on condition
}
do protected work
(optionally, signal or broadcast the condition again or change a predicate value)
unlock the condition

Whenever you use a condition object, the first step is to lock the condition. Locking the condition ensures that your predicate and task code are protected from interference by other threads using the same condition. Once you have completed your task, you can set other predicates or signal other conditions based on the needs of your code. You should always set predicates and signal conditions while holding the condition object’s lock.

When a thread waits on a condition, the condition object unlocks its lock and blocks the thread. When the condition is signaled, the system wakes up the thread. The condition object then reacquires its lock before returning from the wait or waitUntilDate: method. Thus, from the point of view of the thread, it is as if it always held the lock.

A boolean predicate is an important part of the semantics of using conditions because of the way signaling works. Signaling a condition does not guarantee that the condition itself is true. There are timing issues involved in signaling that may cause false signals to appear. Using a predicate ensures that these spurious signals do not cause you to perform work before it is safe to do so. The predicate itself is simply a flag or other variable in your code that you test in order to acquire a Boolean result.

For more information on how to use conditions, see Using POSIX Thread Locks in Threading Programming Guide.

下面是个人的理解以及一些例子

#import <Foundation/Foundation.h>
@interface NSLockTest : NSObject
- (void)forTest;
@end
#import "NSLockTest.h"
@interface NSLockTest()
@property (nonatomic,strong) NSMutableArray *tickets;
@property (nonatomic,assign) int soldCount;
@property (nonatomic,strong) NSCondition *condition;
@end
@implementation NSLockTest
- (void)forTest
{
    self.tickets = [NSMutableArray arrayWithCapacity:1];
    self.condition = [[NSCondition alloc]init];
    NSThread *windowOne = [[NSThread alloc]initWithTarget:self selector:@selector(soldTicket) object:nil];
    [windowOne start];
   
    NSThread *windowTuiPiao = [[NSThread alloc]initWithTarget:self selector:@selector(tuiPiao) object:nil];
    [windowTuiPiao start];

}
-(void)soldTicket
{
    [self.condition lock];
    while (self.tickets.count == 0) {
        NSLog(@"====没票了,等别人退票");
        [self.condition wait];
    }
    NSLog(@"====买了一张票,%@",[self.tickets objectAtIndex:0]);
    [self.tickets removeObjectAtIndex:0];
    [self.condition unlock];
}
- (void)tuiPiao
{
    sleep(3);
    [self.condition lock];
    [self.tickets addObject:@"南京-北京A101(退票)"];
    NSLog(@"====有人退票了,赶快去买");
    [self.condition signal];
    [self.condition unlock];
}
@end

注意点

从上面的例子可以看书

1、NSCondition是条件也是进程,有lock也有unlock

2、有个wait ,一直等待,阻塞线程。

3、signal ,释放信号,wait的线程被激活,继续执行

相关文章

  • iOS同步锁小探

    我测试的iOS同步锁包括@synchronsized、NSLock、NSCondition、NSCondition...

  • 线程锁

    探讨iOS开发中各种锁使用NSCondition实现多线程同步 NSCondition是线程同步, 阻塞线程。 取...

  • iOS锁-NSCondition

    NSCondition 下面是苹果官方文档的说法: A condition variable whose sema...

  • Lock

    iOS中以NS开头常见的锁的有NSCondition、NSConditionLock、NSLock、NSRecur...

  • swift 使用NSCondition自定义实现Dispatch

    材料 NSCondition 条件锁

  • iOS NSCondition详解

    iOS NSCondition讲解 1.定义 官方文档:The NSCondition class impleme...

  • NSCondition、NSConditionLock

    一,NSCondition 1,简介: NSCondition 条件锁,顾名思义,就是满足某些条件才会开锁。NSC...

  • iOS NSCondition使用说明和场景

    1:NSCondition原理和作用 NSCondition 的对象实际上作为一个锁和一个线程检查器:锁主要为了当...

  • iOS-锁-NSCondition&NSConditionLoc

    NSCondition 条件锁,顾名思义,就是满足某些条件才会开锁。NSCondition,可以确保线程仅在满足特...

  • iOS 锁 部分二

    主要讲解NSLock/NSCondition/NSRecursiveLock/锁的基本用法 常见锁的分类: 自旋锁...

网友评论

    本文标题:iOS锁-NSCondition

    本文链接:https://www.haomeiwen.com/subject/fcafpftx.html