美文网首页
iOS 各种锁

iOS 各种锁

作者: 邓布利多教授 | 来源:发表于2022-05-12 10:54 被阅读0次
    #import "ViewController.h"
    #import <libkern/OSAtomic.h>// OSSpinLock头文件
    #import <os/lock.h>// os_unfair_lock头文件
    #import <pthread.h>// pthread_mutex_t头文件
    #import <pthread.h>// pthread_rwlock_t头文件
    
    @interface ViewController ()
    
    @property (nonatomic, strong) UIView *bgView;
    
    @property (nonatomic) int count;
    
    /// 自旋锁-忙等
    @property (nonatomic, assign) OSSpinLock ossLock;
    
    /// NSLock-对pthread_mutex_t的封装
    @property (nonatomic, strong) NSLock *lock;
    
    /// 队列组
    @property (nonatomic, strong) dispatch_group_t grounp;
    
    /// os_unfair_lock-互斥锁
    @property (nonatomic, assign) os_unfair_lock osUnLock;
    
    /// 互斥锁(递归锁:允许同一个线程对一把锁进行加锁;不同线程需要等待)-需要销毁
    @property (nonatomic, assign) pthread_mutex_t mutex;
    @property (nonatomic, assign) pthread_cond_t cond;
    
    /// 递归锁-对pthread_mutex_t的封装
    @property (nonatomic, strong) NSRecursiveLock *reLock;
    
    /// 条件锁-对mutex和cond的封装
    @property (nonatomic, strong) NSCondition *condition;
    
    /// 条件锁-对NSCondition的封装(设置线程依赖)
    @property (nonatomic, strong) NSConditionLock *condLock;
    
    /// 读写锁-需要销毁
    @property (nonatomic, assign) pthread_rwlock_t rwLock;
    
    /// 栅栏dispatch_barrier_async
    
    @end
    
    @implementation ViewController
    
    - (void)dealloc {
        /// 销毁锁
        pthread_mutex_destroy(&_mutex);
        
        /// 销毁条件
        pthread_cond_destroy(&_cond);
        
        /// 销毁读写锁
        pthread_rwlock_destroy(&_rwLock);
    }
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        
        _bgView = [[UIView alloc] init];
        _bgView.frame = CGRectMake(0, 0, 100, 100);
        _bgView.backgroundColor = UIColor.redColor;
        [self.view addSubview:_bgView];
        
        self.ossLock = OS_SPINLOCK_INIT;
        
        self.lock = [[NSLock alloc] init];
        
        self.grounp = dispatch_group_create();
        
        self.osUnLock = OS_UNFAIR_LOCK_INIT;
        
        /// 初始化属性
        pthread_mutexattr_t attr;
        pthread_mutexattr_init(&attr);
        pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_DEFAULT);
        //pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);/// 递归锁
        /// 初始化锁
        pthread_mutex_init(&_mutex, &attr);
        /// 销毁属性
        pthread_mutexattr_destroy(&attr);
        
        /*
         /// 添加条件锁(不同线程执行任务时需要的控制条件,线程依赖,使用场景:可变数组删除和添加元素)
         pthread_cond_init(&_cond, NULL);
         /// ⚠️以下两个方法需要放在加锁解锁中间执行
         /// 根据条件,进入休眠状态,放开当前锁;被唤醒后会再次对当前线程加锁
         pthread_cond_wait(&_cond, &_mutex);
         /// 处理完成,发送信号唤醒锁
         pthread_cond_signal(&_cond);
         /// 处理完成,广播通知等待该条件的所有线程,唤醒锁,和pthread_cond_signal二选一
         pthread_cond_broadcast(&_cond);
         /// ⚠️
         */
        
        self.reLock = [[NSRecursiveLock alloc] init];
        
        self.condition = [[NSCondition alloc] init];
        
        self.condLock = [[NSConditionLock alloc] initWithCondition:1];
        
        pthread_rwlock_init(&_rwLock, NULL);
        
        /*
         /// 读锁
         pthread_rwlock_rdlock(&_rwLock);
         /// 写锁
         pthread_rwlock_wrlock(&_rwLock);
         /// 读写锁解锁
         pthread_rwlock_unlock(&_rwLock);
         */
        
        self.count = 15;
        
        [self test];
        
    }
    
    - (void)test {
        
        dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
    
        dispatch_async(queue, ^{
            for (int i = 0; i < 5; i ++) {
                [self doSomething];
            }
        });
    
        dispatch_async(queue, ^{
            for (int i = 0; i < 5; i ++) {
                [self doSomething];
            }
        });
    
        dispatch_async(queue, ^{
            for (int i = 0; i < 5; i ++) {
                [self doSomething];
            }
        });
        
    //    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
    //
    //    dispatch_group_async(self.grounp, queue, ^{
    //        for (int i = 0; i < 5; i ++) {
    //            [self doSomething];
    //        }
    //    });
    //
    //    dispatch_group_async(self.grounp, queue, ^{
    //        for (int i = 0; i < 5; i ++) {
    //            [self doSomething];
    //        }
    //    });
    //
    //    dispatch_group_async(self.grounp, queue, ^{
    //        for (int i = 0; i < 5; i ++) {
    //            [self doSomething];
    //        }
    //    });
        
    }
    
    - (void)doSomething {
        
    //    OSSpinLockLock(&_ossLock);
        
    //    [self.lock lock];
        
    //    dispatch_group_enter(self.grounp);
        
    //    os_unfair_lock_lock(&_osUnLock);
        
    //    pthread_mutex_lock(&_mutex);
        
    //    [self.reLock lock];
        
    //    [self.condition lock];
    ////    [self.condition wait];
        
        [self.condLock lockWhenCondition:self.condLock.condition];
        
        self.count --;
        NSLog(@"当前线程:%@ 操作剩余 = %d", [NSThread currentThread], self.count);
        
        [self.condLock unlockWithCondition:self.condLock.condition];
        
    ////    [self.condition signal];
    ////    [self.condition broadcast];
    //    [self.condition unlock];
        
    //    [self.reLock unlock];
        
    //    pthread_mutex_unlock(&_mutex);
        
    //    os_unfair_lock_unlock(&_osUnLock);
        
    //    dispatch_group_leave(self.grounp);
        
    //    [self.lock unlock];
        
    //    OSSpinLockUnlock(&_ossLock);
        
    }
    
    @end
    
    
    

    相关文章

      网友评论

          本文标题:iOS 各种锁

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