pthread
pthread是一套通用的多线程API,可以在Unix/Linux/Windows等系统跨平台使用,使用C语言编写,需要程序员自己管理线程的声明周期,使用难度较大,在iOS开发中,几乎不使用pthread。
pthread的使用
- 首先写头文件#import<pthread.h>
- 创建线程,并开启线程执行任务
  pthread-create(&thread, NULL, run, NULL)中个参数含义
- &thread是线程对象,指向线程标识符的指针
- 第二个是 线程属性,默认为NULL
- 第三个run表示指向函数的指针,新创建的线程从run函数地址开始运行
- 第四个默认 为NULL,若上述函数需要参数,将地址传入
pthread其他相关方法
- pthread_create() 创建一个线程
- pthread_exit() 终止当前线程
- pthread_cancel() 中断另一个线程的运行
- pthread_join() 阻塞当前的线程,直到另一个线程运行结束
- pthread_attr_init() 初始化线程的属性
- pthread_attr_setdetachstate() 设置脱离状态的属性(决定这个线程在终止时是否可以被结合)
- pthread_attr_getdetachstate() 获取脱离状态的属性
- pthread_attr_deatory() 删除线程的属性
- pthread_kill() 向线程发送一个信号
NSThread
NSThread是苹果官方提供的,使用起来比pthread更加面向对象,简单易用,可以直接操作线程对象。但也需要程序员自己管理线程的生命周期(主要是创建),我们在开发的过程中偶尔使用NSThread。经常使用[NSThread currentThread]来显示当前线程信息。
创建、启动线程
- 先创建线程、再启动线程
  - 创建队列后自动队列
  - 隐式创建并启动线程
 
线程相关用法

线程状态控制方法
- 启动线程方法
- (void)start; - 线程取消
- (void)cancel 线程进入就绪状态->运行状态。当线程任务执行完毕,自动进入死亡状态` - 阻塞(暂停)线程方法
+ (void)sleepUntilDate:(NSDate *)date; + (void)sleepForTimeInterval:(NSTimeInterval)ti;   - 强制退出线程
+ (void)exit; 线程进入死亡状态
线程之间的通信
在开发中,我们经常会在自线程进行耗时操作,操作结束后再回到主线程去刷新UI。 这就涉及到了子线程和主线程之间的通信。
关于NSthread的线程通信的方法
- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(nullable id)arg waitUntilDone:(BOOL)wait modes:(nullable NSArray<NSString *> *)array;
- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(nullable id)arg waitUntilDone:(BOOL)wait;
wait为YES,要等主线程test方法执行完,子线程才会执行run后面的代码,即在五秒后打印完主线程中的当前线程之后 才会打印end  
而当wait为NO时,子线程不会等待要在主线程执行的方法执行完,会继续执行后面的代码。 (end的打印时机) 
- (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(nullable id)arg waitUntilDone:(BOOL)wait modes:(nullable NSArray<NSString *> *)array API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));
- (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(nullable id)arg waitUntilDone:(BOOL)wait API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));
- (id)performSelector:(SEL)aSelector;
- (id)performSelector:(SEL)aSelector withObject:(id)object;
- (id)performSelector:(SEL)aSelector withObject:(id)object1 withObject:(id)object2;
NSThread线程安全
线程安全解决的方案:给线程加锁,在一个线程执行该操作的时候,不允许其他线程进行操作。iOS实现线程加锁有很多种方式,@synchronized、 NSLock、NSRecursiveLock、NSCondition、NSConditionLock、pthread_mutex、dispatch_semaphore、OSSpinLock、atomic(property) set/get等等各种方式,来保证线程安全,从而解决线程同步问题
参考文章
|