iOSiOS Developer

多线程锁的使用

2017-05-31  本文已影响29人  踏云小子

方法一:用NSLock

TestObj *obj = [[TestObj alloc] init];
//线程1
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [lock lock];
        [obj method1];
        sleep(30);
        [lock unlock];
    });
    
    //线程2
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        sleep(5);//以保证让线程2的代码后执行
        [lock lock];
        [obj method2];
        [lock unlock];
    });

方法二:用@synchronized

TestObj *obj = [[TestObj alloc] init];
    //线程1
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        @synchronized (obj) {
            [obj method1];
        }
        sleep(30);
    });
    
    //线程2
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        sleep(5);//以保证让线程2的代码后执行
        @synchronized (obj) {
            [obj method2];
        }
    });

方法三:用GCD的dispatch_semaphore_t和dispatch_semaphore_wait

    dispatch_semaphore_t semaphore = dispatch_semaphore_create(1);
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
        NSLog(@"method1");
        sleep(10);
        dispatch_semaphore_signal(semaphore);
    });
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        sleep(1);
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
        NSLog(@"method2");
        dispatch_semaphore_signal(semaphore);
    });

这里dispatch_semaphore_create(1)创建的是一个令牌,所以会先执行method1任务,然后等待10秒后发送一个信号(dispatch_semaphore_signal(semaphore))使信号量+1,再执行method2任务

dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
        NSLog(@"method1");
        sleep(10);
        dispatch_semaphore_signal(semaphore);
    });
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        sleep(1);
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
        NSLog(@"method2");
        dispatch_semaphore_signal(semaphore);
    });

这里只执行到dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);,永远不会执行NSLog(@"method1")和下面的method2方法

dispatch_semaphore_t semaphore = dispatch_semaphore_create(2);
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
        NSLog(@"method1");
        sleep(10);
        dispatch_semaphore_signal(semaphore);
    });
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        sleep(1);
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
        NSLog(@"method2");
        dispatch_semaphore_signal(semaphore);
    });

方法三:用pthread_mutex互斥锁

@interface XYThread ()
{
    pthread_mutex_t mutex;  //声明pthread_mutex_t的结构
}
@end

//在viewDidLoad里初始化
- (void)viewDidLoad {
    [super viewDidLoad];
    pthread_mutex_init(&mutex, NULL);
}
//加锁
pthread_mutex_lock(&mutex);

/*你的代码*/

//解锁
pthread_mutex_unlock(&mutex);
- (void)dealloc{
    pthread_mutex_destroy(&mutex);  //释放该锁的数据结构
}

针对函数递归调用的情况,可以使用递归锁

- (void)viewDidLoad {
    NSMutableArray *array = @[].mutableCopy;
    
    pthread_mutexattr_t attr;
    pthread_mutexattr_init(&attr);
    pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL);//定义锁的类别
    __block pthread_mutex_t mutex;
    pthread_mutex_init(&mutex, &attr);
        pthread_mutexattr_destroy (&attr);

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        pthread_mutex_lock(&mutex);
        [array addObject:@"1"];
        [NSThread sleepForTimeInterval:1];
        NSLog(@"thread1");
        pthread_mutex_unlock(&mutex);
    });

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        pthread_mutex_lock(&mutex);
        [array addObject:@"2"];
        [NSThread sleepForTimeInterval:2];
        NSLog(@"thread2");
        pthread_mutex_unlock(&mutex);
    });
}
上一篇下一篇

猜你喜欢

热点阅读