用在线程安全上面pthread_mutex_t

本文介绍了线程调度算法,强调了多线程在CPU资源分配中的角色。重点讲解了iOS中多线程的概念和意义,包括NSThread、GCD的使用。特别提到了在线程安全方面,pthread_mutex_t的重要作用,如在YYKit中的应用,并展示了NSThread和GCD的API示例。

是时间片轮转调度算法和优先级调度算法的综合和发展。

有多个优先级不同的队列,每个队列里面有多个等待线程。
CPU每次从优先级高的遍历到低的,取队首的线程运行,运行完了放回队尾,优先级越高,时间片越短,即响应越快,时间片就不是固定的了。
队列内部还是用先来先服务的策略。

三、多线程工作的概念和意义
多CPU计算机中,各个线程可以占用不同的CPU:因为线程是处理机调度的单位
每个线程都有一个线程ID、线程控制块TCB:类比没有引入线程的进程的进程ID和进程控制块PCB
线程也有运行、就绪、阻塞三种基本状态
线程几乎不拥有系统资源:出了CPU外的系统资源都被分配给了进程,包括一些IO设备、内存地址空间等等
同一进程的不同线程共享进程的资源
由于共享内存地址空间,同一进程中的线程间的通信甚至无需系统干预
同一进程中的线程切换,不会引起进程切换,但是不同进程中的线程切换,则会引起进程切换
切换同一进程中的线程,系统的开销小;而切换不同进程中的线程,系统的开销较大
四、iOS中的线程
根据层级从低到高,分别是NSThread < GCD < NSOperation,下面的三部分,分别围绕着这三中多线程方案来讲述。

4.1 NSThread
说到了NSThread就要提一嘴pthreads,pthread 是一套通用的多线程的 API,可以在Unix / Linux / Windows 等系统跨平台使用,使用 C 语言编写,需要程序员自己管理线程的生命周期,使用难度较大,我们在 iOS 开发中几乎不使用 pthread。
比较典型的有两个例子:

1、用在线程安全上面pthread_mutex_t。

2、用于获取当前的线程,YYKit上有典型用法。

/**
Submits a block for asynchronous execution on a main queue and returns immediately.
*/
static inline void dispatch_async_on_main_queue(void (^block)()) {
if (pthread_main_np()) {
block();
} else {
dispatch_async(dispatch_get_main_queue(), block);
}
}
NSThread 是苹果官方提供的,使用起来比 pthread 更加面向对象,简单易用,可以直接操作线程对象。不过也需要需要程序员自己管理线程的生命周期(主要是创建),我们在开发的过程中偶尔使用 NSThread。比如我们会经常调用 [NSThread currentThread] 来显示当前的进程信息。

  • (instancetype)init API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0)) NS_DESIGNATED_INITIALIZER;

  • (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;
    // equivalent to the first method with kCFRunLoopCommonModes

  • (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));
    // equivalent to the first method with kCFRunLoopCommonModes

  • (void)performSelectorInBackground:(SEL)aSelector withObject:(nullable id)arg API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));
    以上是我们最常用的一些NSThread的API。

4.2 GCD
GCD全称Grand Central Dispatch,是基于 C 实现的一套 API。

image
照例,先来看API:

#include <dispatch/base.h> //引用dispatch的基本枚举以及宏定义
#include <dispatch/time.h> //dispatch中的时间对象dispatch_time_t
#include <dispatch/object.h>
#include <dispatch/queue.h> //dispatch线程调度队列
#include <dispatch/block.h> //dispatch调度块
#include <dispatch/source.h> //协调处理特定低级系统事件的对象。
#include <dispatch/group.h> //dispatch调度组
#include <dispatch/semaphore.h> //dispatch信号量
#include <dispatch/once.h> //dispatch一次执行
#include <dispatch/data.h> //文件的结构体
#include <dispatch/io.h> //读取文件使用
#include <dispatch/workloop.h>

4.2.1 dispatch_async
GCD通过这个API来进行子线程的切换,并且通过设置具体的dispatch_queue_t来来控制任务队列是并行还是串行,同时也可以切换到dispatch_get_main_queue上,因为主线程和主队列是绑定的,于是此操作也代表切换到主线程。

//异步切换到主线程
dispatch_async(dispatch_get_main_queue(), ^{
//实现代码
});
//同步切换到主线程
dispatch_sync(dispatch_get_main_queue(), ^{
//实现代码
});
//异步切换到子线程
dispatch_async(dispatch_get_global_queue(0, 0), ^{
//实现代码
});
//同步切换到子线程
dispatch_sync(dispatch_get_global_queue(0, 0), ^{
//实现代码
});

https://news.hbfzb.com/m/1127/sitemap.html
https://news.hbfzb.com/m/1127/page_9.html
https://news.hbfzb.com/m/1127/page_8.html
https://news.hbfzb.com/m/1127/page_7.html
https://news.hbfzb.com/m/1127/page_65.html
https://news.hbfzb.com/m/1127/page_64.html
https://news.hbfzb.com/m/1127/page_63.html
https://news.hbfzb.com/m/1127/page_62.html
https://news.hbfzb.com/m/1127/page_61.html
https://news.hbfzb.com/m/1127/page_60.html
https://news.hbfzb.com/m/1127/page_6.html
https://news.hbfzb.com/m/1127/page_59.html
https://news.hbfzb.com/m/1127/page_58.html
https://news.hbfzb.com/m/1127/page_57.html
https://news.hbfzb.com/m/1127/page_56.html
https://news.hbfzb.com/m/1127/page_55.html
https://news.hbfzb.com/m/1127/page_54.html
https://news.hbfzb.com/m/1127/page_53.html
https://news.hbfzb.com/m/1127/page_52.html
https://news.hbfzb.com/m/1127/page_51.html
https://news.hbfzb.com/m/1127/page_50.html
https://news.hbfzb.com/m/1127/page_5.html
https://news.hbfzb.com/m/1127/page_49.html
https://news.hbfzb.com/m/1127/page_48.html
https://news.hbfzb.com/m/1127/page_47.html
https://news.hbfzb.com/m/1127/page_46.html
https://news.hbfzb.com/m/1127/page_45.html
https://news.hbfzb.com/m/1127/page_44.html
https://news.hbfzb.com/m/1127/page_43.html
https://news.hbfzb.com/m/1127/page_42.html
https://news.hbfzb.com/m/1127/page_41.html
https://news.hbfzb.com/m/1127/page_40.html
https://news.hbfzb.com/m/1127/page_4.html
https://news.hbfzb.com/m/1127/page_39.html
https://news.hbfzb.com/m/1127/page_38.html
https://news.hbfzb.com/m/1127/page_37.html
https://news.hbfzb.com/m/1127/page_36.html
https://news.hbfzb.com/m/1127/page_35.html
https://news.hbfzb.com/m/1127/page_34.html
https://news.hbfzb.com/m/1127/page_33.html
https://news.hbfzb.com/m/1127/page_32.html
https://news.hbfzb.com/m/1127/page_31.html
https://news.hbfzb.com/m/1127/page_30.html
https://news.hbfzb.com/m/1127/page_3.html
https://news.hbfzb.com/m/1127/page_29.html
https://news.hbfzb.com/m/1127/page_28.html
https://news.hbfzb.com/m/1127/page_27.html
https://news.hbfzb.com/m/1127/page_26.html
https://news.hbfzb.com/m/1127/page_25.html
https://news.hbfzb.com/m/1127/page_24.html
https://news.hbfzb.com/m/1127/page_23.html
https://news.hbfzb.com/m/1127/page_22.html
https://news.hbfzb.com/m/1127/page_21.html
https://news.hbfzb.com/m/1127/page_20.html
https://news.hbfzb.com/m/1127/page_2.html
https://news.hbfzb.com/m/1127/page_19.html
https://news.hbfzb.com/m/1127/page_18.html
https://news.hbfzb.com/m/1127/page_17.html
https://news.hbfzb.com/m/1127/page_16.html
https://news.hbfzb.com/m/1127/page_15.html
https://news.hbfzb.com/m/1127/page_14.html
https://news.hbfzb.com/m/1127/page_13.html
https://news.hbfzb.com/m/1127/page_12.html
https://news.hbfzb.com/m/1127/page_11.html
https://news.hbfzb.com/m/1127/page_10.html

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值