iOS 开发 Objective-C

iOS 底层 day22 多线程 NSConditionLock

2020-09-18  本文已影响0人  望穿秋水小作坊

一、 NSConditionLock

#import "NSConditionLockDemo.h"

@interface NSConditionLockDemo ()
@property(nonatomic, strong)NSConditionLock *conditionLock;
@end

@implementation NSConditionLockDemo
- (instancetype)init
{
    self = [super init];
    if (self) {
        self.conditionLock = [[NSConditionLock alloc] initWithCondition:1];
    }
    return self;
}
- (void)otherTest {
    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
    dispatch_async(queue, ^{
        [self __three];
    });
    dispatch_async(queue, ^{
        [self __one];
    });
    dispatch_async(queue, ^{
        [self __two];
    });
    
}
- (void)__one {
    [self.conditionLock lockWhenCondition:1];
    NSLog(@"%s",__func__);
    [self.conditionLock unlockWithCondition:2];
}
- (void)__two {
    [self.conditionLock lockWhenCondition:2];
    NSLog(@"%s",__func__);
    [self.conditionLock unlockWithCondition:3];
}
- (void)__three {
    [self.conditionLock lockWhenCondition:3];
    NSLog(@"%s",__func__);
    [self.conditionLock unlockWithCondition:1];
}

@end

二、 SerialQueue 串行队列

#import "SerialQueueDemo.h"

@interface SerialQueueDemo ()
@property(nonatomic, strong) dispatch_queue_t serialQueue;
@end

@implementation SerialQueueDemo
- (instancetype)init
{
    self = [super init];
    if (self) {
        self.serialQueue = dispatch_queue_create("serialQueue", DISPATCH_QUEUE_SERIAL);
    }
    return self;
}

- (void)__saleTicket {
    dispatch_sync(self.serialQueue, ^{
        [super __saleTicket];
    });
}
@end

三、 semaphore 信号量

semaphore主要 API
#import "SemaphoreDemo.h"

@interface SemaphoreDemo ()
@property(nonatomic, strong)dispatch_semaphore_t semaphore1;
@property(nonatomic, strong)dispatch_semaphore_t semaphore5;
@end
@implementation SemaphoreDemo
- (instancetype)init
{
    self = [super init];
    if (self) {
        self.semaphore1 = dispatch_semaphore_create(1);
        self.semaphore5 = dispatch_semaphore_create(5);
    }
    return self;
}
- (void)__saleTicket {
    dispatch_semaphore_wait(self.semaphore1, DISPATCH_TIME_FOREVER);
    [super __saleTicket];
    dispatch_semaphore_signal(self.semaphore1);
}

- (void)__test {
    dispatch_semaphore_wait(self.semaphore5, DISPATCH_TIME_FOREVER);
    sleep(2.0);
    NSLog(@"task");
    dispatch_semaphore_signal(self.semaphore5);
}

- (void)otherTest {
    for (int i = 0; i < 20; i++) {
        [[[NSThread alloc] initWithTarget:self selector:@selector(__test) object:nil] start];
    }
}
@end

四、 @sychronized

#import "SynchronizedDemo.h"

@implementation SynchronizedDemo
- (void)__saleTicket {
    @synchronized(self){
        [super __saleTicket];
    }
}
- (void)__saveMoney {
    @synchronized([self class]){
        [super __saveMoney];
    }
}
- (void)__drawMoney {
    @synchronized([self class]){
        [super __drawMoney];
    }
}
@end

五、 自旋锁和互斥锁比较

1. 什么情况使用自旋锁比较划算?
2. 什么情况使用互斥锁比较划算?

六、atomic

七、如何在 iOS 中实现 多读单写?(两种方案)

1. 使用垮平台的 pthread_rwlock_t
#import "PthreadRwlockDemo.h"
#import <pthread.h>

@interface PthreadRwlockDemo ()
@property(nonatomic, assign)pthread_rwlock_t rwlock;
@end

@implementation PthreadRwlockDemo
- (instancetype)init
{
    self = [super init];
    if (self) {
        pthread_rwlock_init(&_rwlock, NULL);
    }
    return self;
}
- (void)otherTest {
    for (int i = 0; i < 10; i++) {
        [[[NSThread alloc] initWithTarget:self selector:@selector(__read) object:nil] start];
        [[[NSThread alloc] initWithTarget:self selector:@selector(__read) object:nil] start];
        [[[NSThread alloc] initWithTarget:self selector:@selector(__write) object:nil] start];

    }
}
- (void)__read {
    pthread_rwlock_rdlock(&_rwlock);
    NSLog(@"%s", __func__);
    pthread_rwlock_unlock(&_rwlock);
}
- (void)__write {
    pthread_rwlock_wrlock(&_rwlock);
    sleep(1.0);
    NSLog(@"%s", __func__);
    pthread_rwlock_unlock(&_rwlock);
}
- (void)dealloc {
    pthread_rwlock_destroy(&_rwlock);
}
@end
2. 使用 iOS 提供的栅栏函数 dispatch_barrier_async
#import "DispatchBarrierAsyncDemo.h"

@interface DispatchBarrierAsyncDemo ()
@property(nonatomic, strong) dispatch_queue_t queue;
@end

@implementation DispatchBarrierAsyncDemo

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.queue = dispatch_queue_create("concurrentQueue", DISPATCH_QUEUE_CONCURRENT);
    }
    return self;
}

- (void)otherTest {
    for (int i = 0; i < 10; i++) {
        [[[NSThread alloc] initWithTarget:self selector:@selector(__read) object:nil] start];
        [[[NSThread alloc] initWithTarget:self selector:@selector(__read) object:nil] start];
        [[[NSThread alloc] initWithTarget:self selector:@selector(__write) object:nil] start];

    }
}
- (void)__read {
    dispatch_async(self.queue, ^{
        NSLog(@"%s", __func__);
    });
}
- (void)__write {
    dispatch_barrier_async(self.queue, ^{
        sleep(1.0);
        NSLog(@"%s", __func__);
    });
}
@end
上一篇 下一篇

猜你喜欢

热点阅读