Skip to content

Commit b97a983

Browse files
committed
semaphore相关函数
1 parent 15464c9 commit b97a983

File tree

1 file changed

+35
-6
lines changed

1 file changed

+35
-6
lines changed

GCD/GCD/GCDAdvancedViewController.m

+35-6
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,47 @@ - (void)viewDidLoad {
2222

2323
- (IBAction)redo:(id)sender {
2424

25-
// [self apply];
25+
// [self applytest];
2626

27-
// [self iterationsManunal];
27+
[self semaphore];
28+
29+
}
30+
31+
// MARK:<dispatch_semaphore_t---信号量相关>
32+
33+
- (void)semaphore{
2834

29-
// [self nastedApplySC];
35+
dispatch_group_t group = dispatch_group_create();
36+
dispatch_semaphore_t semaphore = dispatch_semaphore_create(10);
37+
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
38+
39+
40+
for (int i = 0; i < 100; i++){
41+
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
42+
dispatch_group_async(group, queue, ^{
43+
NSLog(@"%i--%@",i,[NSThread currentThread]);
44+
sleep(2);
45+
dispatch_semaphore_signal(semaphore);
46+
});
47+
}
48+
dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
49+
}
50+
51+
// MARK:<dispatch_apply---apply相关>
3052

31-
// [self nastedApplyCC];
53+
- (void)applytest{
3254

33-
// [self nastedApplySS];
55+
// [self apply];
3456

35-
[self nastedApplyCS];
57+
// [self iterationsManunal];
58+
59+
// [self nastedApplySC];
3660

61+
// [self nastedApplyCC];
62+
63+
// [self nastedApplySS];
64+
65+
[self nastedApplyCS];
3766
}
3867

3968
/*!

0 commit comments

Comments
 (0)