常见锁

302 阅读2分钟

Lock

解决多个线程竞争访问,产生得数据错乱问题.

常见得锁

OSSpinLock 自旋锁,在iOS10已经废弃

顾名思义,会在上锁得地方忙等,不会休眠.
1:会造成优先级反转得问题
    优先级低得如果先加速,那么优先级得高得会在加锁得地方等待,因为优先级高所以cpu会一直处理,那么进入锁得一直都不会解锁,造成死锁.
2:使用方法
 #import <libkern/OSAtomic.h> 头文件
 OSSpinLock lock = OS_SPINLOCK_INIT 初始化
  OSSpinLockLock(&_ticketLock); 加锁
  OSSpinLockUnlock(&_ticketLock);解锁

os_unfair_lock 互斥锁,不公平锁

会在等待解锁的过程中进行休眠.
2:使用方法
 #import <os/lock.h> 头文件
 os_unfair_lock lock =  OS_UNFAIR_LOCK_INIT 初始化
 os_unfair_lock_lock(&_ticketLock); 加锁
 os_unfair_lock_unlock(&_ticketLock);解锁

Pthread_mutex_t

默认锁 互斥锁,也是低级锁,在等待锁得时候进行休眠

#import "YCPhtreadMutex.h"
#import <Pthread.h>
@interface YCPhtreadMutex()
@property(nonatomic,assign)pthread_mutex_t  ticketLock;
@property(nonatomic,assign)pthread_mutex_t  monryLock;
@end
@implementation YCPhtreadMutex
//initMutex
-(void)__initMutex:(pthread_mutex_t *)mutex{
    
    pthread_mutexattr_t attr_t;
    pthread_mutexattr_init(&attr_t);
    ///默认锁
    pthread_mutexattr_settype(&attr_t, PTHREAD_MUTEX_DEFAULT);
    pthread_mutex_init(mutex, &attr_t);
    pthread_mutexattr_destroy(&attr_t);
}
- (instancetype)init{
    if (self = [super init]) {
        [self __initMutex:&_ticketLock];
        [self __initMutex:&_monryLock];
    }
    return self;
}
- (void)_saveMoney{
    pthread_mutex_lock(&_ticketLock);
    [super _saveMoney];
    pthread_mutex_unlock(&_ticketLock);
}
- (void)_drawMoney{
    pthread_mutex_lock(&_monryLock);
    [super _drawMoney];
    pthread_mutex_unlock(&_monryLock);
}
- (void)_sellTickets{
    pthread_mutex_lock(&_monryLock);
    [super _sellTickets];
    pthread_mutex_unlock(&_monryLock);
}
- (void)dealloc
{
    pthread_mutex_destroy(&_monryLock);
    pthread_mutex_destroy(&_ticketLock);
}

递归锁 允许在同一线程对一把锁进行多次加锁,解锁

#import "PthreadRecursive.h"
#import <Pthread.h>
@interface PthreadRecursive()
@property(nonatomic,assign)pthread_mutex_t  recursiceLock;
@end
@implementation PthreadRecursive
//initMutex
-(void)__initMutex:(pthread_mutex_t *)mutex{
    
    pthread_mutexattr_t attr_t;
    pthread_mutexattr_init(&attr_t);
    ///递归锁
    pthread_mutexattr_settype(&attr_t, PTHREAD_MUTEX_RECURSIVE);
    pthread_mutex_init(mutex, &attr_t);
    pthread_mutexattr_destroy(&attr_t);
}
- (instancetype)init{
    if (self = [super init]) {
        [self __initMutex:&_recursiceLock];
    }
    return self;
}
-(void)test1{
    pthread_mutex_lock(&_recursiceLock);
    NSLog(@"sssss");
    [self test2];
    pthread_mutex_lock(&_recursiceLock);
}
-(void)test2{
    pthread_mutex_lock(&_recursiceLock);
      NSLog(@"ssss2");
    pthread_mutex_lock(&_recursiceLock);
}
- (void)dealloc
{
    pthread_mutex_destroy(&_recursiceLock);
}
@end

条件锁

#import "PthreadMutexCondtion.h"
#import <Pthread.h>
@interface PthreadMutexCondtion()
@property(nonatomic,assign)pthread_mutex_t  conditionLock;
@property(nonatomic,assign)pthread_cond_t  cond;
@property(nonatomic,strong)NSMutableArray *datas;
@end
@implementation PthreadMutexCondtion
//initMutex
-(void)__initMutex:(pthread_mutex_t *)mutex{
    
    pthread_mutexattr_t attr_t;
    pthread_mutexattr_init(&attr_t);
    ///递归锁
    pthread_mutexattr_settype(&attr_t, PTHREAD_MUTEX_RECURSIVE);
    pthread_mutex_init(mutex, &attr_t);
    pthread_mutexattr_destroy(&attr_t);
    ///初始化条件
    pthread_cond_init(&_cond, NULL);
  
}
- (instancetype)init{
    if (self = [super init]) {
        self.datas = [NSMutableArray array];
        [self __initMutex:&_conditionLock];
    }
    return self;
}
- (void)test{
    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
    dispatch_async(queue, ^{
        [self test1];
    });
    dispatch_async(queue, ^{
        [self test2];
    });
}
-(void)test1{
    pthread_mutex_lock(&_conditionLock);
    if (self.datas.count == 0) {
        ///当 满足条件得时候,将会等待信号,并解锁
        pthread_cond_wait(&_cond, &_conditionLock);
    }
    [self.datas removeLastObject];
    NSLog(@"删除了元素");
    pthread_mutex_unlock(&_conditionLock);
}
-(void)test2{
    pthread_mutex_lock(&_conditionLock);
    [self.datas addObject:@"123"];
    ///发送信号
    pthread_cond_signal(&_cond);
    NSLog(@"添加了元素");
    pthread_mutex_unlock(&_conditionLock);
}
- (void)dealloc
{
    pthread_mutex_destroy(&_conditionLock);
      pthread_cond_destroy(&_cond);
}
@end

NSLock 封装得pthread_mutex得default模式

NSLock *lock=[[NSLock alloc] init];
[lock lock];
[lock unlock];

NSRecursiveLock 递归锁 封装得pthread_mutex得recursive模式

NSRecursiveLock *lock=[[NSRecursiveLock alloc] init];
[lock lock];
[lock unlock];

NSCondition 条件锁 封装得pthread_mutex_cond和pthread_mutex_lock

NSCondition *lock=[[NSCondition alloc] init];
[lock lock];
[lock wait];
[lock single];
[lock broadcast];///广播
[lock unlock];

NSConditionLock 条件锁,可以传入条件进行判断

#import "NSCodintionLockDemo.h"
@interface NSCodintionLockDemo()
@property(nonatomic,strong)NSConditionLock  *conditionLock;
@property(nonatomic,strong)NSMutableArray *datas;
@end
@implementation NSCodintionLockDemo

- (instancetype)init{
    if (self = [super init]) {
        self.datas = [NSMutableArray array];
        self.conditionLock = [[NSConditionLock alloc] initWithCondition:1];
    }
    return self;
}
- (void)test{
    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
    dispatch_async(queue, ^{
        [self test1];
    });
    dispatch_async(queue, ^{
        [self test2];
    });
}
-(void)test1{
    [self.conditionLock lockWhenCondition:2];
    [self.datas removeLastObject];
    NSLog(@"删除了元素");
    [self.conditionLock unlock];
}
-(void)test2{
    [self.conditionLock lockWhenCondition:1];
    [self.datas addObject:@"123"];
    NSLog(@"添加了元素");
    [self.conditionLock unlockWithCondition:2];
}


@end

dispatch_semaphore_t

///<#long value#> 代表最大并发量
 dispatch_semaphore_t semaphore =  dispatch_semaphore_create(<#long value#>)
 ///执行一次信号量将会减一
  dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
  ///发送一次 信号量+1
  dispatch_semaphore_signal(semaphore);

串行对列

@synchornized