-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNSConditionLockTest.m
More file actions
67 lines (50 loc) · 1.51 KB
/
NSConditionLockTest.m
File metadata and controls
67 lines (50 loc) · 1.51 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//
// NSConditionLockTest.m
// WGFcodeNotes
//
// Created by 白菜 on 2022/10/10.
// Copyright © 2022 WG. All rights reserved.
//
#import "NSConditionLockTest.h"
@interface NSConditionLockTest()
@property(nonatomic, strong) NSConditionLock *lock;
@end
@implementation NSConditionLockTest
-(instancetype)init {
self = [super init];
if (self) {
//初始化条件锁,传入初始化条件值 如果没有传递条件值,则默认的条件值是: 0
self.lock = [[NSConditionLock alloc]initWithCondition:1];
// self.lock = [[NSConditionLock alloc]init];
}
return self;
}
-(void)otherTest {
dispatch_queue_t queue = dispatch_queue_create("myQueue", DISPATCH_QUEUE_SERIAL);
[[[NSThread alloc]initWithTarget:self selector:@selector(__one) object:nil] start];
[[[NSThread alloc]initWithTarget:self selector:@selector(__two) object:nil] start];
[[[NSThread alloc]initWithTarget:self selector:@selector(__three) object:nil] start];
}
-(void)__one{
//加锁(当这个锁的内部条件值==1时才开始加锁)
[self.lock lockWhenCondition:1];
sleep(3);
NSLog(@"__one--");
//设置内部条件值为2,并且将这把锁放开
[self.lock unlockWithCondition:2];
}
-(void)__two {
//加锁
[self.lock lockWhenCondition:2];
NSLog(@"__two--");
//解锁
[self.lock unlockWithCondition:3];
}
-(void)__three {
//加锁
[self.lock lockWhenCondition:3];
NSLog(@"__three--");
//解锁
[self.lock unlock];
}
@end