/// 栅栏函数、线程安全锁
- (void)barrier {
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
dispatch_async(queue, ^{
sleep(2);
NSLog(@"1");
});
dispatch_async(queue, ^{
NSLog(@"2");
});
dispatch_barrier_async(queue, ^{
NSLog(@"barrier");
});
dispatch_async(queue, ^{
NSLog(@"3");
});
dispatch_async(queue, ^{
NSLog(@"4");
});
}
/*
执行结果1:
2022-08-21 18:19:24.949868+0800 YDBasicPublicCourse[49563:3947304] 2
2022-08-21 18:19:24.949868+0800 YDBasicPublicCourse[49563:3947306] barrier
2022-08-21 18:19:24.949878+0800 YDBasicPublicCourse[49563:3947308] 3
2022-08-21 18:19:24.950120+0800 YDBasicPublicCourse[49563:3947306] 4
2022-08-21 18:19:26.950422+0800 YDBasicPublicCourse[49563:3947310] 1
执行结果2:
2022-08-21 18:20:27.169850+0800 YDBasicPublicCourse[49573:3949082] barrier
2022-08-21 18:20:27.169850+0800 YDBasicPublicCourse[49573:3949081] 2
2022-08-21 18:20:27.169864+0800 YDBasicPublicCourse[49573:3949085] 3
2022-08-21 18:20:27.170101+0800 YDBasicPublicCourse[49573:3949081] 4
2022-08-21 18:20:29.173572+0800 YDBasicPublicCourse[49573:3949086] 1
*/
// 栅栏函数、线程安全锁_需要自定义队列
- (void)barrier {
dispatch_queue_t queue = dispatch_queue_create(NULL, DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue, ^{
sleep(2);
NSLog(@"1");
});
dispatch_async(queue, ^{
NSLog(@"2");
});
dispatch_barrier_async(queue, ^{
NSLog(@"barrier");
});
dispatch_async(queue, ^{
NSLog(@"3");
});
dispatch_async(queue, ^{
NSLog(@"4");
});
}
/*
执行结果:
2022-08-21 18:21:46.955971+0800 YDBasicPublicCourse[49595:3951260] 2
2022-08-21 18:21:48.956457+0800 YDBasicPublicCourse[49595:3951263] 1
2022-08-21 18:21:48.956695+0800 YDBasicPublicCourse[49595:3951263] barrier
2022-08-21 18:21:48.956841+0800 YDBasicPublicCourse[49595:3951263] 3
2022-08-21 18:21:48.956902+0800 YDBasicPublicCourse[49595:3951261] 4
*/
/// 线程安全:内存的数据被多个线程读写,出现的结果如果是可预见的,锁:保护线程安全——>确保只有一条线程在操作数据
// 互斥锁
- (void)synchronized {
dispatch_async(dispatch_get_global_queue(0, 0), ^{
@synchronized (self) {
NSLog(@"1");
sleep(2);
NSLog(@"1_ok");
}
});
dispatch_async(dispatch_get_global_queue(0, 0), ^{
// 锁:写法最简单,但是效率最低。
@synchronized (self) {
NSLog(@"2");
sleep(2);
NSLog(@"2_ok");
}
});
}
/*
2022-08-21 18:49:30.473397+0800 YDBasicPublicCourse[49842:3984355] 1
2022-08-21 18:49:32.474201+0800 YDBasicPublicCourse[49842:3984355] 1_ok
2022-08-21 18:49:32.474489+0800 YDBasicPublicCourse[49842:3984345] 2
2022-08-21 18:49:34.478394+0800 YDBasicPublicCourse[49842:3984345] 2_ok
*/
// 信号量:自旋锁
- (void)semaphore {
dispatch_semaphore_t semaphore = dispatch_semaphore_create(1);
dispatch_async(dispatch_get_global_queue(0, 0), ^{
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
NSLog(@"1");
sleep(2);
NSLog(@"1_ok");
dispatch_semaphore_signal(semaphore);
});
dispatch_async(dispatch_get_global_queue(0, 0), ^{
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
NSLog(@"2");
sleep(2);
NSLog(@"2_ok");
dispatch_semaphore_signal(semaphore);
});
}
/*
2022-08-21 18:57:52.967842+0800 YDBasicPublicCourse[49925:3995254] 1
2022-08-21 18:57:54.971486+0800 YDBasicPublicCourse[49925:3995254] 1_ok
2022-08-21 18:57:54.971875+0800 YDBasicPublicCourse[49925:3995253] 2
2022-08-21 18:57:56.972118+0800 YDBasicPublicCourse[49925:3995253] 2_ok
*/
// 信号量:自旋锁
- (void)semaphore {
// 此处参数为1、是否可以理解为线程数量
dispatch_semaphore_t semaphore = dispatch_semaphore_create(2);
dispatch_async(dispatch_get_global_queue(0, 0), ^{
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
NSLog(@"1");
sleep(2);
NSLog(@"1_ok");
dispatch_semaphore_signal(semaphore);
});
dispatch_async(dispatch_get_global_queue(0, 0), ^{
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
NSLog(@"2");
sleep(2);
NSLog(@"2_ok");
dispatch_semaphore_signal(semaphore);
});
}
/*
2022-08-21 20:38:36.790768+0800 YDBasicPublicCourse[50583:4102255] 2
2022-08-21 20:38:36.790762+0800 YDBasicPublicCourse[50583:4102259] 1
2022-08-21 20:38:38.791510+0800 YDBasicPublicCourse[50583:4102255] 2_ok
2022-08-21 20:38:38.791524+0800 YDBasicPublicCourse[50583:4102259] 1_ok
*/
/*
semaphore:自旋锁-其他线程正在执行我们锁定的代码,其他线程就会进入死循环等待。
@synchronized:互斥锁-其他线程正在执行我们的锁定代码,其他线程就会进入休眠。
**/
// 生成一个随机数
- (void)testDispatchApply {
_number = 0;
dispatch_apply(10000, dispatch_get_global_queue(0, 0), ^(size_t iteration) {
self.number++;
});
NSLog(@"%d",self.number);
}
/*
2022-08-21 21:20:12.443660+0800 YDBasicPublicCourse[50909:4149925] 6312
*/
// 生成一个随机数_加锁处理《atomic仅在set和get方法里进行加锁处理》
- (void)testDispatchApply {
_number = 0;
NSLock *lock = [[NSLock alloc] init];
dispatch_apply(10000, dispatch_get_global_queue(0, 0), ^(size_t iteration) {
[lock lock];
self.number++;
[lock unlock];
});
NSLog(@"%d",self.number);
}
/*
2022-08-21 21:23:44.793507+0800 YDBasicPublicCourse[50947:4154783] 10000
*/
代码块
代码块
代码块