1 深拷贝浅拷贝
2 分类不能添加成员变量
3 单例标准写法
4 weak弱引用
字符串拷贝分析
// 字符串拷贝分析
NSString *str = @"testStr";
NSString *str1 = [str copy];
NSMutableString *str2 = [str mutableCopy];
NSLog(@"str:%@,%p", str, str); // str: testStr ,0x10ac23020
NSLog(@"str1:%@,%p", str1, str1); // str1:testStr ,0x10ac23020
NSLog(@"str2:%@,%p", str2, str2); // str2:testStr ,0x60000112b720
// 总结: string不可变字符串 copy浅拷贝 mutableCopy深拷贝
NSMutableString *mutStr = [[NSMutableString alloc] initWithString:@"123"];
NSString *mutStrCopy = [mutStr copy];
NSMutableString *mutStrMutCopy = [mutStr mutableCopy];
NSLog(@"mutStr:%@,%p", mutStr, mutStr); // mutStr:123, 0x6000039cf6c0
NSLog(@"mutStrCopy:%@,%p", mutStrCopy, mutStrCopy); // mutStrCopy:123, 0x984f4eaff626b56e
NSLog(@"mutStrMutCopy:%@,%p", mutStrMutCopy, mutStrMutCopy); // mutStrMutCopy:123,0x6000039ce850
// 总结: mutableString可变字符串 copy 和 mutableCopy 都是深拷贝
}
属性字符串深/浅拷贝
@property (nonatomic, strong) NSString* strString;
@property (nonatomic, copy) NSString* copString;
NSMutableString* textString = [[NSMutableString alloc] initWithString:@"str"];
self.strString = textString; // strong
self.copString = textString; // copy
NSLog(@"\n初始值\ntextSting:%@,\nstrongString:%@,\ncopyString:%@",textString,_strString,_copString);
/*
初始值
textSting: str,
strongString:str,
copyString: str
*/
NSLog(@"\n初始内存地址\ntextSting:%p,\nstrongString:%p,\ncopyString:%p",textString,_strString,_copString);
/*
初始内存地址
textSting: 0x60000198d500,
strongString:0x60000198d500,
copyString: 0x9a6b751948ecb1a9 */
[textString appendString:@"str"];
NSLog(@"\n修改后值\ntextSting:%@,\nstrongString:%@,\ncopyString:%@",textString,_strString,_copString);
/*修改后值
textSting: strstr,
strongString:strstr,
copyString: str */
NSLog(@"\n修改后内存地址\ntextSting:%p,\nstrongString:%p,\ncopyString:%p",textString,_strString,_copString);
/*
修改后内存地址
textSting: 0x60000198d500,
strongString: 0x60000198d500,
copyString: 0x9a6b751948ecb1a9
*/
/**
总结: strong 浅拷贝 copy深拷贝
*/
数组拷贝分析
NSMutableString *testStr = [@"123" mutableCopy];
// array可变数组
NSArray *arr = @[@"1", @"2", testStr];
// arr = @[@"1", @"2", @"123"]
NSArray *arrCopy = [arr copy];
NSMutableArray *arrMutablecopy = [arr mutableCopy];
NSLog(@"===arr:%@,%p \nobjectP:%p", arr, arr, arr[0]); // 原数组 array
// arr:(1, 2) ,0x600002979e60 , objectP:0x10623c168
NSLog(@"arrCopy:%@,%p \nobjectP:%p", arrCopy, arrCopy, arrCopy[0]); // copy后
// arr:(1, 2) ,0x600002979e60 , objectP:0x10623c168
NSLog(@"arrMutablecopy:%@,%p \nobjectP:%p", arrMutablecopy, arrMutablecopy, arrMutablecopy[0]); // mutableCopy后
// arrMutablecopy:(1,2),0x60000270a430 , objectP:0x10623c168
// 总结 不可变数组, copy浅拷贝, mutableCopy深拷贝
[testStr appendString:@"45"];
NSLog(@"arr:%@,%p \nobjectP:%p", arr, arr, arr[0]); //原数组 array 元素修改后
// arr:(1, 2, 12345) ,0x600002979e60 , objectP:0x10623c168
NSLog(@"arrCopy:%@,%p \nobjectP:%p", arrCopy, arrCopy, arrCopy[0]); // copy后
// arr:(1, 2, 12345) ,0x600002979e60 , objectP:0x10623c168
NSLog(@"arrMutablecopy:%@,%p \nobjectP:%p", arrMutablecopy, arrMutablecopy, arrMutablecopy[0]); // mutableCopy后
// arrMutablecopy:(1,2,12345),0x60000270a430 , objectP:0x10623c168
// 修改元素后总结 不可变数组, copy浅拷贝, mutableCopy深拷贝
// 总结 对array copy 浅拷贝,mutableCopy 深拷贝 ,但是,mutableCopy后,arrMutablecopy里面的元素 与 原arr里面的元素地址相同,所以array的mutableCopy是不完全深拷贝,只深拷贝了第一层,没有深拷贝内层
/*=============================================================================================*/
NSMutableString *mutarrTestStr = [@"abc" mutableCopy];
// mutableArray 不可变数组
NSMutableArray *mutarr = [NSMutableArray arrayWithObjects:@"123", mutarrTestStr, nil];
// mutarr = @[@"123", @"abc"]
NSArray *mutarrCopy = [mutarr copy];
NSMutableArray *mutarrMutablecopy = [mutarr mutableCopy];
NSLog(@"mutarr:%@,%p \nobjectP:%p", mutarr, mutarr, mutarr[0]); // 原数组 mutableArray
// arr:(123, abc) ,0x600001396460 , objectP:0x1015bc0e8
NSLog(@"mutarrCopy:%@,%p \nobjectP:%p", mutarrCopy, mutarrCopy, mutarrCopy[0]); // copy后
// arr:(123, abc) ,0x600001396460 , objectP:0x1015bc0e8
NSLog(@"mutarrMutablecopy:%@,%p \nobjectP:%p", mutarrMutablecopy, mutarrMutablecopy, mutarrMutablecopy[0]); // mutableCopy后
// arrMutablecopy:(123,abc),0x60000270a430 , objectP:0x1015bc0e8
// 总结 数组copy 浅拷贝; mutableCopy 深拷贝, 数组元素浅拷贝(都一样)
[mutarrTestStr appendString:@"456"]; //数组 元素修改后
// mutarr = @[@"123", @"abc456"]
NSLog(@"mutarr:%@,%p \nobjectP:%p", mutarr, mutarr, mutarr[0]); //原数组 mutableArray 元素修改后
// arr:(123, abc456) ,0x600001396460 , objectP:0x1015bc0e8
NSLog(@"mutarrCopy:%@,%p \nobjectP:%p", mutarrCopy, mutarrCopy, mutarrCopy[0]); // copy后
// arr:(123, abc456) ,0x600001396460 , objectP:0x1015bc0e8
NSLog(@"mutarrMutablecopy:%@,%p \nobjectP:%p", mutarrMutablecopy, mutarrMutablecopy, mutarrMutablecopy[0]); // mutableCopy后
// arrMutablecopy:(123,abc456),0x60000270a430 , objectP:0x1015bc0e8
// 总结 数组元素变动 和上面地址一样
// 1.对于不可变对象而言,copy便是浅拷贝,mutableCopy是深拷贝
// 2.对于可变对象而言,copy和mutableCopy都是深拷贝,但是copy出来的对象是不可变的,而mutableCopy出来的对象是可变的
// 3. 对于容器类型来说,mutableCopy 仅仅拷贝了第一层,所以是不完全深拷贝
属性数组 深/浅拷贝
@property (nonatomic, strong) NSArray* strongArray;
@property (nonatomic, copy) NSArray* copyedArray;
// 一 可变数组
NSMutableArray *muArray = [NSMutableArray arrayWithArray:@[@"1",@"2",@"3"]];
self.strongArray = muArray;
self.copyedArray = muArray;
NSLog(@"muArray地址:%p,数据:%@",muArray,muArray); // muArray地址: 0x600000bafde0, 数据:(1, 2, 3 )
NSLog(@"strongArray地址:%p,数据:%@",_strongArray,_strongArray); // strongArray地址:0x600000bafde0, 数据:(1, 2, 3 )
NSLog(@"copyedArray地址:%p,数据:%@",_copyedArray,_copyedArray); // copyedArray地址:0x600000bafbd0, 数据:(1, 2, 3 )
[muArray addObject:@"4"]; // 数据修改后
NSLog(@"muArray地址:%p,数据:%@",muArray,muArray); // muArray地址: 0x600000bafde0, 数据:(1, 2, 3 , 4)
NSLog(@"strongArray地址:%p,数据:%@",_strongArray,_strongArray); // strongArray地址:0x600000bafde0, 数据:(1, 2, 3 , 4)
NSLog(@"copyedArray地址:%p,数据:%@",_copyedArray,_copyedArray); // copyedArray地址:0x600000bafbd0, 数据:(1, 2, 3 )
//总结: 可变数组 赋值: strongArray 浅拷贝 copyedArray 深拷贝
// 二 不可变数组
NSArray *array = @[@"1",@"2",@"3"];
self.strongArray = array;
self.copyedArray = array;
NSLog(@"array地址:%p,数据:%@",array,array); // array地址: 0x6000035be340, 数据:(1, 2, 3 )
NSLog(@"strongArray地址:%p,数据:%@",_strongArray,_strongArray); // strongArray地址:0x6000035be340, 数据:(1, 2, 3 )
NSLog(@"copyedArray地址:%p,数据:%@",_copyedArray,_copyedArray); // copyedArray地址:0x6000035be340, 数据:(1, 2, 3 )
//总结: 不可变数组 赋值: strongArray 浅拷贝 copyedArray 浅拷贝
link: https://www.jianshu.com/p/7c38856b3231
分类 属性
Objective-C类是由Class类型来表示的,它实际上是一个指向objc_class结构体的指针。它的定义如下:
typedef struct objc_class *Class;
objc_class结构体的定义如下:
struct objc_class {
Class isa OBJC_ISA_AVAILABILITY; // isa指针
#if !__OBJC2__
Class super_class OBJC2_UNAVAILABLE; // 父类
const char *name OBJC2_UNAVAILABLE; // 类名
long version OBJC2_UNAVAILABLE; // 类的版本信息
long info OBJC2_UNAVAILABLE; // 类信息
long instance_size OBJC2_UNAVAILABLE; // 该类的实例变量大小
struct objc_ivar_list *ivars OBJC2_UNAVAILABLE; // 该类的成员变量链表
struct objc_method_list **methodLists OBJC2_UNAVAILABLE; // 方法定义的链表
struct objc_cache *cache OBJC2_UNAVAILABLE; // 方法缓存
struct objc_protocol_list *protocols OBJC2_UNAVAILABLE; // 协议链表
#endif
}
为什么不能添加成员变量呢?
在上面的objc_class结构体中,
ivars是objc_ivar_list(成员变量列表)指针;
methodLists是指向objc_method_list指针的指针。
在Runtime中,objc_class结构体大小是固定的,long instance_size 标识实例变量大小,不可能往这个结构体中添加数据,只能修改。所以ivars指向的是一个固定区域,只能修改成员变量值,不能增加成员变量个数。methodList是一个二维数组,所以可以修改*methodLists的值来增加成员方法,虽没办法扩展methodLists指向的内存区域,却可以改变这个内存区域的值(存储的是指针)。因此,可以动态添加方法,不能添加成员变量。
link https://www.jianshu.com/p/e203d70ccc3f
单例
#import "FTManager.h"
static FTManager *manager = nil;
@implementation FTManager
+ (instancetype)shareManager {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
// manager = [[FTManager alloc] init];
manager = [[super allocWithZone:NULL] init] ;
});
return manager;
}
// 保证 [[FTManager alloc] init] 对象地址统一
+ (instancetype)allocWithZone:(struct _NSZone *)zone {
return [FTManager shareManager];
}
// 保证 [[[FTManager alloc] init] copy] 对象地址统一
- (nonnull id)copyWithZone:(nullable NSZone *)zone {
return [FTManager shareManager];
}
@end
NSLog(@"%p",[FTManager shareManager]);
NSLog(@"%p",[[FTManager alloc] init]);
NSLog(@"%p",[[[FTManager alloc] init] copy]);
2022-04-19 14:14:41.308590+0800 ForTest[3773:68879] 0x6000014faa30
2022-04-19 14:14:41.308763+0800 ForTest[3773:68879] 0x6000014faa30
2022-04-19 14:14:41.308916+0800 ForTest[3773:68879] 0x6000014faa30
weak
1.weak的作用
weak 关键字的作用弱引用,所引用对象的计数器不会加一,并在引用对象被释放的时候自动被设置为 nil。
2.weak的实现原理
第一、通过weak编译解析,可以看出来weak通过runtime初始化的并维护的;
第二、weak和strong都是Object-C的修饰词,而strong是通过runtime维护的一个自动计数表结构。
综上:weak是有Runtime维护的weak表。
3.weak释放为nil过程
weak被释放为nil,需要对对象整个释放过程了解,如下是对象释放的整体流程:
1、调用objc_release
2、因为对象的引用计数为0,所以执行dealloc
3、在dealloc中,调用了_objc_rootDealloc函数
4、在_objc_rootDealloc中,调用了object_dispose函数
5、调用objc_destructInstance
6、最后调用objc_clear_deallocating。
https://www.jianshu.com/p/f331bd5ce8f8
信号量
在IOS系统GCD的semaphore.h头文件中提供三个方法进行PV操作
// 这value是初始化多少个信号量
1.dispatch_semaphore_create(long value);
// 这个方法是P操作对信号量减一,dsema这个参数表示对哪个信号量进行减一,如果该信号量为0则等待,timeout这个参数则是传入等待的时长。
2.dispatch_semaphore_wait(dispatch_semaphore_t dsema, dispatch_time_t timeout);
// 这个方法是V操作对信号量加一,dsema这个参数表示对哪个信号量进行加一
3.dispatch_semaphore_signal(dispatch_semaphore_t dsema);
@interface FTTestViewController ()
/// iphone的数量
@property (nonatomic,assign) int iphoneNumber;
/// 互斥用的信号量
@property (nonatomic,strong) dispatch_semaphore_t semaphore;
@end
@implementation FTTestViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self test];
}
- (void)test {
self.iphoneNumber = 10;
// 初始化1个信号量
self.semaphore = dispatch_semaphore_create(1);
/// 通过信号量进行互斥,开启三个窗口(线程)同时卖iphone
NSThread *thread1 = [[NSThread alloc] initWithTarget:self selector:@selector(sellIphone) object:nil];
thread1.name = @"窗口1";
NSThread *thread2 = [[NSThread alloc] initWithTarget:self selector:@selector(sellIphone) object:nil];
thread2.name = @"窗口2";
NSThread *thread3 = [[NSThread alloc] initWithTarget:self selector:@selector(sellIphone) object:nil];
thread3.name = @"窗口3";
[thread1 start];
[thread2 start];
[thread3 start];
}
// 通过信号量达到互斥
- (void)sellIphone
{
while (1) {
// P操作对信号量进行减一,然后信号量变0,限制其他窗口(线程)进入
dispatch_semaphore_wait(self.semaphore, DISPATCH_TIME_FOREVER);
if (self.iphoneNumber > 0) // 检查还有没iphone可卖
{
NSLog(@"卖出iphone剩下%d台iphone---fromThread:%@",--self.iphoneNumber,[NSThread currentThread]);
}
else
{
NSLog(@"iphone没有库存了");
return;
}
// V操作对信号量进行加一,然后信号量为1,其他窗口(线程)就能进入了
dispatch_semaphore_signal(self.semaphore);
}
}
2022-04-20 14:15:38.484988+0800 ForTest[19430:548682] 卖出iphone剩下9台iphone---fromThread:<NSThread: 0x600001cbbd80>{number = 9, name = 窗口3}
2022-04-20 14:15:38.485890+0800 ForTest[19430:548680] 卖出iphone剩下8台iphone---fromThread:<NSThread: 0x600001cbbd00>{number = 7, name = 窗口1}
2022-04-20 14:15:38.487929+0800 ForTest[19430:548681] 卖出iphone剩下7台iphone---fromThread:<NSThread: 0x600001cbbd40>{number = 8, name = 窗口2}
2022-04-20 14:15:38.489221+0800 ForTest[19430:548682] 卖出iphone剩下6台iphone---fromThread:<NSThread: 0x600001cbbd80>{number = 9, name = 窗口3}
2022-04-20 14:15:38.490322+0800 ForTest[19430:548680] 卖出iphone剩下5台iphone---fromThread:<NSThread: 0x600001cbbd00>{number = 7, name = 窗口1}
2022-04-20 14:15:38.491334+0800 ForTest[19430:548681] 卖出iphone剩下4台iphone---fromThread:<NSThread: 0x600001cbbd40>{number = 8, name = 窗口2}
2022-04-20 14:15:38.492223+0800 ForTest[19430:548682] 卖出iphone剩下3台iphone---fromThread:<NSThread: 0x600001cbbd80>{number = 9, name = 窗口3}
2022-04-20 14:15:38.492808+0800 ForTest[19430:548680] 卖出iphone剩下2台iphone---fromThread:<NSThread: 0x600001cbbd00>{number = 7, name = 窗口1}
2022-04-20 14:15:38.493185+0800 ForTest[19430:548681] 卖出iphone剩下1台iphone---fromThread:<NSThread: 0x600001cbbd40>{number = 8, name = 窗口2}
2022-04-20 14:15:38.498118+0800 ForTest[19430:548682] 卖出iphone剩下0台iphone---fromThread:<NSThread: 0x600001cbbd80>{number = 9, name = 窗口3}
2022-04-20 14:15:38.499101+0800 ForTest[19430:548680] iphone没有库存了
/// 通过同步锁进行互斥,通过同步锁会比通过信号量控制的方式多进入该临界代码(线程数量-1)次
- (void)sellIphoneWithSynchronization
{
while (1) {
@synchronized (self) {
if (self.iphoneNumber > 0) // 检查还有没iphone可卖
{
NSLog(@"%@卖出iphone剩下%d台iphone",[NSThread currentThread].name,--self.iphoneNumber);
}
else
{
NSLog(@"iphone没有库存了");
return;
}
}
}
}
@end
link https://blog.csdn.net/fanyun_01/article/details/111878096
队列 队列组 栅栏函数 线程死锁
队列
/* ##### 队列任务 */
// 一: 同步串行队列
// 参数1 队列名字 参数2 串行队列宏
dispatch_queue_t queue1 = dispatch_queue_create("myQueue", DISPATCH_QUEUE_SERIAL);
for(NSInteger i = 0; i < 10; i++){
// 同步任务 添加到串行队列
dispatch_sync(queue1, ^{
NSLog(@"thread == %@ i====%ld",[NSThread currentThread],(long)i);
// thread == <_NSMainThread: 0x6000002c0000>{number = 1, name = main} i====8
// thread == <_NSMainThread: 0x6000002c0000>{number = 1, name = main} i====9
});
}
// ps: 主线程 同步串行执行任务
// 二: 同步并行队列
dispatch_queue_t queue2 = dispatch_queue_create("myQueue", DISPATCH_QUEUE_CONCURRENT);
for(NSInteger i = 0; i < 10; i++){
dispatch_sync(queue2, ^{
NSLog(@"thread == %@ i====%ld",[NSThread currentThread],(long)i);
});
}
// ps: 主线程 同步执行任务
//三: 异步串行队列
dispatch_queue_t queue3 = dispatch_queue_create("myQueue", DISPATCH_QUEUE_SERIAL);
for(NSInteger i = 0; i < 10; i++){
dispatch_async(queue3, ^{
NSLog(@"thread == %@ i====%ld",[NSThread currentThread],(long)i);
});
}
// ps: 开辟一条子线程 串行执行任务
//四: 异步并行队列
dispatch_queue_t queue4 = dispatch_queue_create("myQueue", DISPATCH_QUEUE_CONCURRENT);
for(NSInteger i = 0; i < 10; i++){
dispatch_async(queue4, ^{
NSLog(@"thread == %@ i====%ld",[NSThread currentThread],(long)i);
});
// thread == <NSThread: 0x600001104700>{number = 4, name = (null)} i====7
// thread == <NSThread: 0x6000024cb900>{number = 11, name = (null)} i====8
// thread == <NSThread: 0x6000024f5340>{number = 5, name = (null)} i====9
}
// ps: 开辟多条子线程 并行执行任务
// 开辟线程主要看 dispatch_async 异步
//五: 异步 主队列
dispatch_queue_t queue5 = dispatch_get_main_queue();
for(NSInteger i = 0; i < 10; i++){
sleep(2);
dispatch_async(queue5, ^{
NSLog(@"thread == %@ i====%ld",[NSThread currentThread],(long)i);
});
}
// ps: 主线程 串行执行任务
队列组
// 六: 队列组
dispatch_group_t group = dispatch_group_create();
dispatch_queue_t concurrentQueue = dispatch_queue_create("concurrentQueue", DISPATCH_QUEUE_CONCURRENT);
dispatch_group_async(group, concurrentQueue, ^{
for (int i = 0; i < 3; i++)
{
NSLog(@"Task1 %@ %d", [NSThread currentThread], i);
}
});
dispatch_group_async(group, dispatch_get_main_queue(), ^{
for (int i = 0; i < 3; i++)
{
NSLog(@"Task2 %@ %d", [NSThread currentThread], i);
}
});
dispatch_group_async(group, concurrentQueue, ^{
for (int i = 0; i < 3; i++)
{
NSLog(@"Task3 %@ %d", [NSThread currentThread], i);
} //
});
dispatch_group_notify(group, concurrentQueue, ^{
NSLog(@"All Task Complete");
});
Task3 <NSThread: 0x600000bcf400>{number = 5, name = (null)} 0
Task1 <NSThread: 0x600000b8a740>{number = 8, name = (null)} 0
Task3 <NSThread: 0x600000bcf400>{number = 5, name = (null)} 1
Task1 <NSThread: 0x600000b8a740>{number = 8, name = (null)} 1
Task3 <NSThread: 0x600000bcf400>{number = 5, name = (null)} 2
Task1 <NSThread: 0x600000b8a740>{number = 8, name = (null)} 2
Task2 <_NSMainThread: 0x600000be8980>{number = 1, name = main} 0
Task2 <_NSMainThread: 0x600000be8980>{number = 1, name = main} 1
Task2 <_NSMainThread: 0x600000be8980>{number = 1, name = main} 2
All Task Complete
// ps 异步并行队列 正常会开启n调线程 这里只开启了一条线程???
栅栏函数
栅栏函数的作⽤:
1 实现多读单写, 当锁用
2 控制线程执行顺序, 达到同步的效果
栅栏函数注意:
1 栅栏函数只能控制同一并发队列
2 栅栏函数要想起作用,那么创建任务队列就必须使用dispatch_queue_create()该种方式,使用全局队列栅栏函数无法发挥作用;
dispatch_barrier_async:前面的任务执行完毕,才会来到这里
dispatch_barrier_sync:作用相同,但是这个会堵塞线程,影响后面的执行
@interface FTTestViewController ()
{
// 定义一个并发队列
dispatch_queue_t _concurrent_queue;
// 用户数据中心, 可能多个线程需要数据访问
NSMutableDictionary *_userCenterDic;
}
- (void)viewDidLoad {
[super viewDidLoad];
//七: 栅栏函数
// 多读单写
// 通过宏定义 DISPATCH_QUEUE_CONCURRENT 创建一个并发队列
_concurrent_queue = dispatch_queue_create("read_write_queue", DISPATCH_QUEUE_CONCURRENT);
// 创建数据容器
_userCenterDic = [NSMutableDictionary dictionary];
[self barrierTest]; // 栅栏函数测试
}
- (id)objectForKey:(NSString *)key
{
__block id obj;
// 同步读取指定数据,立刻返回读取结果
dispatch_sync(_concurrent_queue, ^{
obj = [self->_userCenterDic objectForKey:key];
});
return obj;
}
- (void)setObject:(id)obj forKey:(NSString *)key
{
// 异步栅栏调用设置数据
dispatch_barrier_async(_concurrent_queue, ^{
[self->_userCenterDic setObject:obj forKey:key];
});
}
// 栅栏函数测试 举例
- (void)barrierTest {
// 建立全局并发队列
dispatch_queue_t concurrentQueue = dispatch_queue_create("concurrentQueue", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(concurrentQueue, ^{
NSLog(@"任务(一)%@",[NSThread currentThread]);
});
dispatch_async(concurrentQueue, ^{
NSLog(@"任务(二)%@",[NSThread currentThread]);
});
dispatch_async(concurrentQueue, ^{
NSLog(@"任务(三)%@",[NSThread currentThread]);
});
dispatch_barrier_async(concurrentQueue, ^{
NSLog(@"(栅栏函数)%@",[NSThread currentThread]);
});
dispatch_async(concurrentQueue, ^{
NSLog(@"任务(四)%@",[NSThread currentThread]);
});
NSLog(@"开始工作");
}
2022-04-20 19:11:49.205886+0800 ForTest[26101:755643] 开始工作
2022-04-20 19:11:49.206003+0800 ForTest[26101:755903] 任务(二)<NSThread: 0x600002ceb140>{number = 4, name = (null)}
2022-04-20 19:11:49.206010+0800 ForTest[26101:755908] 任务(一)<NSThread: 0x600002c9a1c0>{number = 6, name = (null)}
2022-04-20 19:11:49.206041+0800 ForTest[26101:755901] 任务(三)<NSThread: 0x600002c89c00>{number = 3, name = (null)}
2022-04-20 19:11:49.206232+0800 ForTest[26101:755908] (栅栏函数)<NSThread: 0x600002c9a1c0>{number = 6, name = (null)}
2022-04-20 19:11:49.206398+0800 ForTest[26101:755901] 任务(四)<NSThread: 0x600002c89c00>{number = 3, name = (null)}
线程死锁
触发场景: 主线程/同步 串行队列/同步 死锁
dispatch_queue_t queue = dispatch_get_main_queue();
dispatch_sync(queue, ^{
NSLog(@"执行任务2"); // Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
});// 往主线程里面 同步添加任务 会发生死锁现象
dispatch_queue_t myQueue = dispatch_queue_create("myQueue", DISPATCH_QUEUE_SERIAL);
dispatch_async(myQueue, ^{ // 异步串行队列
NSLog(@"1111,thread====%@",[NSThread currentThread]);
dispatch_sync(myQueue, ^{ // 同步串行队列
NSLog(@"2222,thread====%@",[NSThread currentThread]);
// Thread 4: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) crash
});
});