iOS锁-NSConditionLock

NSConditionLock

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

A lock that can be associated with specific, user-defined conditions.

Overview

Using an NSConditionLock 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.

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

#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) NSConditionLock *condition;
@end
@implementation NSLockTest
- (void)forTest
{
    self.tickets = [NSMutableArray arrayWithCapacity:1];
    self.condition = [[NSConditionLock alloc]initWithCondition:0];
    NSThread *windowOne = [[NSThread alloc]initWithTarget:self selector:@selector(soldTicketOne) object:nil];
    [windowOne start];
    
    NSThread *windowTwo = [[NSThread alloc]initWithTarget:self selector:@selector(soldTicketTwo) object:nil];
    [windowTwo start];
   
    NSThread *windowTuiPiao = [[NSThread alloc]initWithTarget:self selector:@selector(tuiPiao) object:nil];
    [windowTuiPiao start];

}
//一号窗口
-(void)soldTicketOne
{
    while (YES) {
        NSLog(@"====一号窗口没票了,等别人退票");
        [self.condition lockWhenCondition:1];
        NSLog(@"====在一号窗口买了一张票,%@",[self.tickets objectAtIndex:0]);
        [self.tickets removeObjectAtIndex:0];
        [self.condition unlockWithCondition:0];
    }
}
//二号窗口
-(void)soldTicketTwo
{
    while (YES) {
        NSLog(@"====二号窗口没票了,等别人退票");
        [self.condition lockWhenCondition:2];
        NSLog(@"====在二号窗口买了一张票,%@",[self.tickets objectAtIndex:0]);
        [self.tickets removeObjectAtIndex:0];
        [self.condition unlockWithCondition:0];
    }
}
- (void)tuiPiao
{
    while (YES) {
        sleep(3);
        [self.condition lockWhenCondition:0];
        [self.tickets addObject:@"南京-北京(退票)"];
        int x = arc4random() % 2;
        if (x == 1) {
            NSLog(@"====有人退票了,赶快去一号窗口买");
            [self.condition unlockWithCondition:1];
        }else
        {
            NSLog(@"====有人退票了,赶快去二号窗口买");
            [self.condition unlockWithCondition:2];
        }
    }
    
}
@end

设计了一个例子,有一号售票窗口和二号售票窗口两个窗口可以买票,也会有一个退票窗口,但是退票窗口随机选择退到一号或者二号售票窗口。

NSConditionLock与NSCondition大体相同,但是NSConditionLock可以设置锁条件,而NSCondition确只是无脑的通知信号。

- (void)lockWhenCondition:(NSInteger)condition;

- (void)unlockWithCondition:(NSInteger)condition;

这两个condition一样的时候会相互通知。

过程

1、初始化 self.condition = [[NSConditionLock alloc]initWithCondition:0];

2、获得锁 [self.condition lockWhenCondition:1];

3、解锁 [self.condition unlockWithCondition:1];

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 虽然,2017年的1月份都快过完了,但是按照中国人的传统,过了春节才算进入了新的一年。家家洗洗涮涮,置办年货,张灯...
    白熊妈妈阅读 498评论 4 4
  • 慢慢地,草黄了,风凉了,衣多了,我们知道,美丽的秋姑娘已经向我们走来了。 我漫步在树林中,首先映入眼帘的是一片灿烂...
    乔翕阅读 1,307评论 0 0
  • 周六的早上八点,还有很多人上班,地铁人很多,我上了最后一节车厢。 明明中间很空,但人都都站到了门边,我只能贴着门。...
    独角猫猫阅读 974评论 0 0