-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReadWriteLockTest1.m
More file actions
49 lines (41 loc) · 872 Bytes
/
ReadWriteLockTest1.m
File metadata and controls
49 lines (41 loc) · 872 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//
// ReadWriteLockTest1.m
// WGFcodeNotes
//
// Created by 白菜 on 2022/10/10.
// Copyright © 2022 WG. All rights reserved.
//
#import "ReadWriteLockTest1.h"
@interface ReadWriteLockTest1()
@property(nonatomic, strong) dispatch_queue_t queue;
@end
@implementation ReadWriteLockTest1
-(instancetype)init {
self = [super init];
if (self) {
//初始化并发队列
self.queue = dispatch_queue_create("rw_queue", DISPATCH_QUEUE_CONCURRENT);
}
return self;
}
-(void)otherTest {
for (int i = 0 ; i < 10; i++) {
[self __write];
[self __read];
[self __read];
[self __read];
}
}
-(void)__read{
dispatch_async(_queue, ^{
sleep(1.0);
NSLog(@"read");
});
}
-(void)__write {
dispatch_barrier_async(_queue, ^{
sleep(1.0);
NSLog(@"write");
});
}
@end