使用NSCondition实现多线程同步

iOS中实现多线程技术有非常多方法。

这里说说使用NSCondition实现多线程同步的问题,也就是解决生产者消费者问题(如收发同步等等)。


问题流程例如以下:

    消费者取得锁,取产品,假设没有,则wait,这时会释放锁,直到有线程唤醒它去消费产品;

    生产者制造产品,首先也要取得锁,然后生产,再发signal,这样可唤醒wait的消费者。


这里须要注意wait和signal的问题:

    1: 事实上,wait函数内部悄悄的调用了unlock函数(推測,有兴趣可自行分析),也就是说在调用wati函数后,这个NSCondition对象就处于了无锁的状态,这样其它线程就能够对此对象加锁并触发该NSCondition对象。当NSCondition被其它线程触发时,在wait函数内部得到此事件被触发的通知,然后对此事件又一次调用lock函数(推測),而在外部看起来好像接收事件的线程(调用wait的线程)从来没有放开NSCondition对象的全部权,wati线程直接由堵塞状态进入了触发状态一样。这里easy造成误解。

    2: wait函数并非全然可信的。也就是说wait返回后,并不代表相应的事件一定被触发了,因此,为了保证线程之间的同步关系,使用NSCondtion时往往须要增加一个额外的变量来对非正常的wait返回进行规避。

   3: 关于多个wait时的调用顺序,測试发现与wait运行顺序有关。详细请查阅文档。



前往github下载測试project:

https://github.com/zcsoft/ZCTest_NSCondtion


主要代码片段:

//
//  ViewController.m
//  CondtionTest
//
//  Created by cuibo on 11/12/14.
//  Copyright (c) 2014 cuibo. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property(strong, nonatomic)NSCondition *condition;
@property(strong, nonatomic)NSMutableArray *products;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    
    self.products = [[NSMutableArray alloc] init];
    self.condition = [[NSCondition alloc] init];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//点一下表示開始接受
- (IBAction)test1Button:(id)sender
{
    [NSThread detachNewThreadSelector:@selector(thread1) toTarget:self withObject:nil];
}

//点一下表示收到一个数据
- (IBAction)test2Button:(id)sender
{
    [NSThread detachNewThreadSelector:@selector(thread2) toTarget:self withObject:nil];
}

//发送线程
- (void)thread1
{
    while (1)
    {
        NSLog(@"thread1:等待发送");
        [self.condition lock];
        [self.condition wait];
        
        NSLog(@"thread1:发送");
        [self.condition unlock];
    }
}

//接收线程
- (void)thread2
{
    [self.condition lock];
    NSLog(@"thread2:收到数据");
    [self.condition signal];
    [self.condition unlock];
}


@end




原文地址:https://www.cnblogs.com/blfshiye/p/4550387.html