美文网首页
线程同步

线程同步

作者: 蓝汐o | 来源:发表于2019-06-04 17:11 被阅读0次

线程锁(同时只允许一个线程访问)


#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];
 ..............
}

相关文章

网友评论

      本文标题:线程同步

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