线程锁(同时只允许一个线程访问)
#define SemaphoreBegin \
static dispatch_semaphore_t semaphore; \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
semaphore = dispatch_semaphore_create(1); \
}); \
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
#define SemaphoreEnd \
dispatch_semaphore_signal(semaphore);
条件锁,(可以锁线程,线程依赖)
wait方法:等待信号
signal方法:发送信号,子线程同时开启运行
@property (strong, nonatomic) NSCondition *rotateRouteCond; // 先有线路,再垂直显示
- (void)rotateRouteVertical{
[[[NSThread alloc] initWithTarget:self selector:@selector(rotate) object:nil] start];
}
- (void)rotate{
[self.rotateRouteCond lock];
[self.rotateRouteCond waitUntilDate:[NSDate dateWithTimeIntervalSinceNow:5]];
..............
}
- (void)other{
..............
[self.rotateRouteCond signal];
..............
}
网友评论