iOS多线程-线程安全

2019-06-02  本文已影响0人  学习天亦

复习下线程的基础知识, 这里主要是参考文顶顶多线程篇复习写的。

一、多线程的安全隐患

资源共享
1块资源可能会被多个线程共享,也就是多个线程可能会访问同一块资源
比如多个线程访问同一个对象、同一个变量、同一个文件

当多个线程访问同一块资源时,很容易引发数据错乱和数据安全问题

示例一

示例二

问题代码

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic, strong) NSThread *thread1;
@property (nonatomic, strong) NSThread *thread2;
@property (nonatomic, strong) NSThread *thread3;

/**
 *  剩余票数
 */
@property (nonatomic, assign) int leftTicketCount;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.leftTicketCount = 10;
    
    self.thread1 = [[NSThread alloc] initWithTarget:self selector:@selector(saleTicket) object:nil];
    self.thread1.name = @"1号窗口";
    
    self.thread2 = [[NSThread alloc] initWithTarget:self selector:@selector(saleTicket) object:nil];
    self.thread2.name = @"2号窗口";
    
    self.thread3 = [[NSThread alloc] initWithTarget:self selector:@selector(saleTicket) object:nil];
    self.thread3.name = @"3号窗口";
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.thread1 start];
    [self.thread2 start];
    [self.thread3 start];
}

/**
 *  卖票
 */
- (void)saleTicket {
    while (1) {
        int count = self.leftTicketCount;
        if (count > 0) {
            [NSThread sleepForTimeInterval:0.05];
            
            self.leftTicketCount = count - 1;
            
            NSLog(@"%@卖了一张票, 剩余%d张票", [NSThread currentThread].name, self.leftTicketCount);
        } else {
            return; // 退出循环
        }
    }
}

@end

二、安全隐患分析


三、安全隐患解决 – 互斥锁

相关专业术语:线程同步
线程同步的意思是:多条线程按顺序地执行任务
互斥锁,就是使用了线程同步技术

代码示例

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic, strong) NSThread *thread1;
@property (nonatomic, strong) NSThread *thread2;
@property (nonatomic, strong) NSThread *thread3;

/**
 *  剩余票数
 */
@property (nonatomic, assign) int leftTicketCount;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.leftTicketCount = 10;
    
    self.thread1 = [[NSThread alloc] initWithTarget:self selector:@selector(saleTicket) object:nil];
    self.thread1.name = @"1号窗口";
    
    self.thread2 = [[NSThread alloc] initWithTarget:self selector:@selector(saleTicket) object:nil];
    self.thread2.name = @"2号窗口";
    
    self.thread3 = [[NSThread alloc] initWithTarget:self selector:@selector(saleTicket) object:nil];
    self.thread3.name = @"3号窗口";
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.thread1 start];
    [self.thread2 start];
    [self.thread3 start];
}

/**
 *  卖票
 */
- (void)saleTicket {
    while (1) {
        @synchronized (self) {//加了这句
            int count = self.leftTicketCount;
            if (count > 0) {
                [NSThread sleepForTimeInterval:0.05];
                
                self.leftTicketCount = count - 1;
                
                NSLog(@"%@卖了一张票, 剩余%d张票", [NSThread currentThread].name, self.leftTicketCount);
            } else {
                return; // 退出循环
            }
        }
    }
}

@end

四、原子和非原子属性

OC在定义属性时有nonatomic和atomic两种选择

nonatomic和atomic对比

iOS开发的建议

atomic加锁原理

@property (assign, atomic) int age;

- (void)setAge:(int)age { 
    @synchronized(self) { 
       _age = age;
    }
}
上一篇下一篇

猜你喜欢

热点阅读