多读单写 phtread_rwlock_t & dispatch
2023-06-16 本文已影响0人
xxttw
- IO操作
- 文件操作
- 多读单写
phtread_rwlock_t
@property (nonatomic, assign) pthread_rwlock_t rwlock;
pthread_rwlock_rdlock(&_rwlock);
sleep(2);
NSLog(@"%s", __func__);
pthread_rwlock_unlock(&_rwlock);
pthread_rwlock_wrlock(&_rwlock);
sleep(1);
NSLog(@"%s", __func__);
pthread_rwlock_unlock(&_rwlock);
- (void)dealloc {
pthread_rwlock_destroy(&_rwlock);
}
dispatch_barrier_async
- 异步栅栏仅对自定义并发队列(DISPATCH_QUEUE_CONCURRENT)才会有栅栏的作用
dispatch_queue_t queue = dispatch_queue_create("myqueue",DISPATCH_QUEUE_CONCURRENT)
如果是串行队列,或全局并发队列,等同于dispatch_async
函数
image.png
self.queue = dispatch_queue_create("com", DISPATCH_QUEUE_CONCURRENT);
for (int i = 0; i < 10; i++) {
[self read];
[self read];
[self read];
[self write];
}
- (void)read {
dispatch_async(self.queue, ^{
sleep(1);
NSLog(@"read");
});
}
- (void)write {
dispatch_barrier_async(self.queue, ^{
sleep(1);
NSLog(@"write");
});
}
2022-12-20 18:00:54.042267+0800 多线程-读写锁[2957:10175394] read
2022-12-20 18:00:54.042345+0800 多线程-读写锁[2957:10175395] read
2022-12-20 18:00:54.042381+0800 多线程-读写锁[2957:10175388] read
2022-12-20 18:00:55.050300+0800 多线程-读写锁[2957:10175388] write
2022-12-20 18:00:56.051883+0800 多线程-读写锁[2957:10175388] read
2022-12-20 18:00:56.051883+0800 多线程-读写锁[2957:10175395] read
2022-12-20 18:00:56.055010+0800 多线程-读写锁[2957:10175393] read
2022-12-20 18:00:57.061040+0800 多线程-读写锁[2957:10175393] write
2022-12-20 18:00:58.065408+0800 多线程-读写锁[2957:10175388] read
2022-12-20 18:00:58.065365+0800 多线程-读写锁[2957:10175393] read
2022-12-20 18:00:58.065458+0800 多线程-读写锁[2957:10175395] read
2022-12-20 18:00:59.066764+0800 多线程-读写锁[2957:10175395] write
dispatch_barriarr_sync
和 dispatch_barriarr_async
的区别在于是否会阻塞
当前线程
如果是dispatch_barriarr_sync
10-15的循环会先执行, 1在同步栅栏调用结束后打印, sync会阻塞1的线程
如果是dispatch_barrier_async
1会直接被打印, 因为1在其他线程, 10-15在子线程,async不阻塞1的线程
self.queue = dispatch_queue_create("com", DISPATCH_QUEUE_CONCURRENT);
for (int i = 0; i < 5; i++) {
[self read];
}
[self write];
NSLog(@"1 - %@", [NSThread currentThread]);
for (int i = 10; i < 15; i++) {
[self read];
}
}
- (void)read {
dispatch_async(self.queue, ^{
sleep(1);
NSLog(@"read");
});
}
- (void)write {
dispatch_barrier_sync(self.queue, ^{
sleep(1);
NSLog(@"write");
});
}
022-12-20 18:29:02.596902+0800 多线程-读写锁[8327:10211413] read
2022-12-20 18:29:02.599688+0800 多线程-读写锁[8327:10211422] read
2022-12-20 18:29:02.599796+0800 多线程-读写锁[8327:10211414] read
2022-12-20 18:29:02.599806+0800 多线程-读写锁[8327:10211416] read
2022-12-20 18:29:02.599815+0800 多线程-读写锁[8327:10211418] read
2022-12-20 18:29:03.601354+0800 多线程-读写锁[8327:10211171] write
2022-12-20 18:29:03.601453+0800 多线程-读写锁[8327:10211171] 1 - <_NSMainThread: 0x600002150800>{number = 1, name = main}
2022-12-20 18:29:04.602319+0800 多线程-读写锁[8327:10211414] read
2022-12-20 18:29:04.602808+0800 多线程-读写锁[8327:10211422] read
2022-12-20 18:29:04.602820+0800 多线程-读写锁[8327:10211419] read
2022-12-20 18:29:04.602829+0800 多线程-读写锁[8327:10211413] read
2022-12-20 18:29:04.604251+0800 多线程-读写锁[8327:10211418] read
- (void)write {
dispatch_barrier_async(self.queue, ^{
sleep(1);
NSLog(@"write");
});
}
2022-12-20 18:30:14.218615+0800 多线程-读写锁[8556:10212848] 1 - <_NSMainThread: 0x6000034c40c0>{number = 1, name = main}
2022-12-20 18:30:15.220370+0800 多线程-读写锁[8556:10213054] read
2022-12-20 18:30:15.229358+0800 多线程-读写锁[8556:10213062] read
2022-12-20 18:30:15.229418+0800 多线程-读写锁[8556:10213057] read
2022-12-20 18:30:15.229442+0800 多线程-读写锁[8556:10213055] read
2022-12-20 18:30:15.229430+0800 多线程-读写锁[8556:10213060] read
2022-12-20 18:30:16.235090+0800 多线程-读写锁[8556:10213060] write
2022-12-20 18:30:17.241020+0800 多线程-读写锁[8556:10213060] read
2022-12-20 18:30:17.241121+0800 多线程-读写锁[8556:10213055] read
2022-12-20 18:30:17.241140+0800 多线程-读写锁[8556:10213057] read
2022-12-20 18:30:17.241160+0800 多线程-读写锁[8556:10213062] read
2022-12-20 18:30:17.241178+0800 多线程-读写锁[8556:10213061] read