NSCondition

时间:2021-03-20 16:02:44

一、NSCondition是对条件变量互斥量的一个封装,用于线程之间的同步。

  其中的互斥量用于保护对条件变量的修改,条件变量变化以信号量的方式通知其它线程实现线程之间的同步。

  

二、NSCondition的API

  NSCondition实现了NSLocking协议,这个协议定义了lock和unlock方法,用于多个线程互斥得执行一段代码,以保护共享的数据。

  同时,NSCondition还包含以下方法:

  - (void)wait;

  - (BOOL)waitUntilDate:(NSDate *)limit;

  - (void)signal;

  - (void)broadcast;

  

  wait:释放互斥量,使当前线程等待,切换到其它线程执行。

  waitUntilDate:释放互斥量,使当前线程等待到某一个时间,切换到其它线程执行。

  signal:唤醒一个其它等待该条件变量的线程

  broadcast:唤醒所有其它等待该条件变量的线程

三、demo

  下面的例子来源于网络,对其做了一点修改

  

 int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here... NSConditionSample *sample = [[NSConditionSample alloc] init]; [NSThread detachNewThreadSelector:@selector(createConsumenr)
toTarget:sample
withObject:nil]; [NSThread detachNewThreadSelector:@selector(createProducter)
toTarget:sample
withObject:nil]; getchar();
}
return ;
}

  下面是生产者、消费者:

 #import "NSConditionSample.h"

 @interface NSConditionSample ()
{
NSCondition *condition;
NSMutableArray *products;
} @end @implementation NSConditionSample - (instancetype)init
{
if(self = [super init])
{
condition = [[NSCondition alloc] init];
products = [[NSMutableArray alloc] init];
}
return self;
} - (void)createConsumenr
{
while (YES) {
NSLog(@"createConsumenr before lock");
[condition lock];
NSLog(@"createConsumenr after lock");
while ([products count] == ) {
NSLog(@"wait for products");
[condition wait];
}
[products removeObjectAtIndex:];
NSLog(@"comsume a product");
[condition unlock];
}
} - (void)createProducter
{
while (YES) {
NSLog(@"createProducter before lock");
[condition lock];
NSLog(@"createProducter after lock");
[products addObject:[[NSObject alloc] init]];
NSLog(@"produce a product");
[condition signal];
[condition unlock];
} } @end

执行的log:

2015-07-24 11:05:48.374 NSConditionSample[45259:40628634] createConsumenr before lock

2015-07-24 11:05:48.374 NSConditionSample[45259:40628635] createProducter before lock

2015-07-24 11:05:48.374 NSConditionSample[45259:40628634] createConsumenr after lock

2015-07-24 11:05:48.375 NSConditionSample[45259:40628634] wait for products

2015-07-24 11:05:48.375 NSConditionSample[45259:40628635] createProducter after lock

2015-07-24 11:05:48.375 NSConditionSample[45259:40628635] produce a product

2015-07-24 11:05:48.375 NSConditionSample[45259:40628635] createProducter before lock

2015-07-24 11:05:48.375 NSConditionSample[45259:40628634] comsume a product

2015-07-24 11:05:48.375 NSConditionSample[45259:40628634] createConsumenr before lock

2015-07-24 11:05:48.375 NSConditionSample[45259:40628635] createProducter after lock

2015-07-24 11:05:48.375 NSConditionSample[45259:40628635] produce a product

2015-07-24 11:05:48.375 NSConditionSample[45259:40628634] createConsumenr after lock

2015-07-24 11:05:48.375 NSConditionSample[45259:40628635] createProducter before lock

2015-07-24 11:05:48.375 NSConditionSample[45259:40628634] comsume a product

2015-07-24 11:05:48.376 NSConditionSample[45259:40628634] createConsumenr before lock

2015-07-24 11:05:48.376 NSConditionSample[45259:40628635] createProducter after lock

2015-07-24 11:05:48.376 NSConditionSample[45259:40628635] produce a product

2015-07-24 11:05:48.376 NSConditionSample[45259:40628634] createConsumenr after lock

2015-07-24 11:05:48.376 NSConditionSample[45259:40628635] createProducter before lock

2015-07-24 11:05:48.376 NSConditionSample[45259:40628634] comsume a product

2015-07-24 11:05:48.390 NSConditionSample[45259:40628634] createConsumenr before lock

2015-07-24 11:05:48.390 NSConditionSample[45259:40628635] createProducter after lock

2015-07-24 11:05:48.390 NSConditionSample[45259:40628635] produce a product