美文网首页乔帮主的遗产
iOS多线程下的数据安全

iOS多线程下的数据安全

作者: 赵哥窟 | 来源:发表于2018-08-09 10:06 被阅读7次
多线程操作共享资源的问题
  • 在多线程的环境下,共享的资源可能会被多个线程共享,也就是多个线程可能会操作同一块资源.
  • 当多个线程操作同一块资源时,很容易引发数据错乱和数据安全问题,数据有可能丢失,有可能增加,有可能错乱.
  • 经典案例 : 火车站卖票,商品抢购
  • 线程安全:同一块资源,被多个线程同时读写操作时,任然能够得到正确的结果,称之为线程是安全的.
如何解决

在多线程操作过程中,如何保护共享数据,其实已经是一个众所周知的事情了,这里总结下自己试过的处理方法:

  • @synchronized
  • NSLock
  • dispatch_semaphore_signal
@synchronized

的作用是创建一个互斥锁,保证此时没有其它线程对self对象进行修改。这个是objective-c的一个锁定令牌,防止self对象在同一时间内被其它线程访问,起到线程的保护作用。
互斥锁的优缺点:
优点:能有效防止因多线程抢夺资源造成的数据安全问题;
缺点:需要消耗大量的CPU资源。

dispatch_semaphore

dispatch_semaphore是GCD用来同步的一种方式,与他相关的共有三个函数,分别是
dispatch_semaphore_create,dispatch_semaphore_signal,dispatch_semaphore_wait。

(1)dispatch_semaphore_create的声明为:
dispatch_semaphore_t dispatch_semaphore_create(long value);
传入的参数为long,输出一个dispatch_semaphore_t类型且值为value的信号量。
值得注意的是,这里的传入的参数value必须大于或等于0,否则dispatch_semaphore_create会返回NULL。

(2)dispatch_semaphore_signal的声明为:
dispatch_semaphore_signal(semaphore);
这个函数会使传入的信号量semaphore的值加1;

(3) dispatch_semaphore_wait的声明为:
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
这个函数会使传入的信号量semaphore的值减1;这个函数的作用是这样的,如果semaphore信号量的值大于0,该函数所处线程就继续执行下面的语句,并且将信号量的值减1;如果semaphore的值为0,那么这个函数就阻塞当前线程等待timeout

下面以典案例购买火车票为例来说明用法

@interface ViewController ()
{
    dispatch_semaphore_t semaphore;
}

@property (assign, nonatomic) NSInteger ticketNumber;
@property (strong, nonatomic) NSLock *lock;

@end

@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.ticketNumber = 100;
    self.lock = [[NSLock alloc]init];
    semaphore = dispatch_semaphore_create(1);
    
    for (NSInteger i = 0; i < 10; i++) {
        NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(sellTicketsWithNSLock) object:nil];
        [thread setName:[NSString stringWithFormat:@"售票员-%zd",i]];
        [thread start];
    }
}
@end
使用Synchronized
- (void)sellTicketsWithSynchronized
{
    while (true) {
        @synchronized(self){
            if (self.ticketNumber > 0) {
                self.ticketNumber --;
                NSThread *thread = [NSThread currentThread];
                NSLog(@"%@卖了一张票,还剩%ld张票",[thread name],self.ticketNumber);
            }else{
                // 退出当前线程
                [NSThread exit];
            }
        }
    }
}
使用NSLock
- (void)sellTicketsWithNSLock
{
    while (true) {
        [self.lock lock];
        if (self.ticketNumber > 0) {
            self.ticketNumber --;
            NSThread *thread = [NSThread currentThread];
            NSLog(@"%@卖了一张票,还剩%ld张票",[thread name],self.ticketNumber);
        }else{
            // 退出当前线程
            [NSThread exit];
        }
         [self.lock unlock]; // 解锁
    }
}
使用Semaphore
- (void)sellTicketsWithSemaphore
{
    while (true) {
        //信号==0就阻塞当前线程等待timeout,>0就继续执行下面的语句信号量的值减1
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
        if (self.ticketNumber > 0) {
            self.ticketNumber --;
            NSThread *thread = [NSThread currentThread];
            NSLog(@"%@卖了一张票,还剩%ld张票",[thread name],self.ticketNumber);
        }else{
            // 退出当前线程
            [NSThread exit];
        }
        // 信号 +1
        dispatch_semaphore_signal(semaphore);
    }
}

demo地址:https://github.com/destinyzhao/ThreadSafeDemo

相关文章

  • iOS多线程下的数据安全

    多线程操作共享资源的问题 在多线程的环境下,共享的资源可能会被多个线程共享,也就是多个线程可能会操作同一块资源. ...

  • iOS多线程下的数据安全

    在多线程操作过程中,往往一个数据同时被多个线程读写,在这种情况下,如果没有相应的机制对数据进行保护,就很可能会发生...

  • iOS多线程到底不安全在哪里?

    iOS多线程到底不安全在哪里? iOS多线程到底不安全在哪里?

  • OC--各种线程锁

    参考:正确使用多线程同步锁@synchronized()iOS中的锁iOS多线程安全详解iOS 常见知识点(三):...

  • 线程安全知多少

    1. 如何定义线程安全 线程安全,拆开来看: 线程:指多线程的应用场景下。 安全:指数据安全。 多线程就不用过多介...

  • iOS 多线程与安全

    iOS 多线程与安全 多核cpu,使用多线程,能明显提升性能,但如果使用不当,会造成严重的后果,轻则数据问题,重则...

  • 02.线程安全性问题

    [TOC] 安全性问题概述 什么是安全性问题 多线程情况下的安全问题,是指数据的一致性问题,在多线程环境下,多个线...

  • iOS 多线程

    iOS中的各种锁iOS多线程到底不安全在哪里?

  • iOS线程安全——锁

    摘自《iOS开发快速进阶与实战》 线程安全是iOS开发中避免不了的话题,随着多线程的使用,对于资源的竞争以及数据的...

  • 【iOS开发】iOS中的线程安全

    参考文章:iOS开发多线程篇—线程安全

网友评论

    本文标题:iOS多线程下的数据安全

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