iOS---多线程实现方案一 (pthread、NSThread)

在iOS开发中,多线程是我们在开发中经常使用的一门技术。那么本文章将和大家探讨一下针对于多线程的技术实现。本文主要分为如下几个部分:

  • iOS开发中实现多线程的方式

  • 单线程

  • pthread

  • NSThread

一、iOS开发中实现多线程的方式

  • pthread: 跨平台,适用于多种操作系统,可移植性强,是一套纯C语言的通用API,且线程的生命周期需要程序员自己管理,使用难度较大,所以在实际开发中通常不使用。

  • NSThread: 基于OC语言的API,使得其简单易用,面向对象操作。线程的声明周期由程序员管理,在实际开发中偶尔使用。

  • GCD: 基于C语言的API,充分利用设备的多核,旨在替换NSThread等线程技术。线程的生命周期由系统自动管理,在实际开发中经常使用。

  • NSOperation: 基于OC语言API,底层是GCD,增加了一些更加简单易用的功能,使用更加面向对象。线程生命周期由系统自动管理,在实际开发中经常使用。

二、单线程

进程,线程的概念就不在此文进行介绍了。至于提到单线程,只是想跟大家聊一聊如果没有多线程开发我们的项目将是怎么样的。作为一个ios开发人员,大家都知道主线程是用来刷新UI的,而线程又是串行的。也就是说,我们的程序运行在主线程上,如果此时迎来了一些耗时操作的时候,我们的手机屏幕会卡住,所以多线程开发是很必要的。本文的ThreadDemo工作组中有一个在主线程耗时操作的demo,名称为TimeConsumingDemo。大家看一下就好了,demo很简单,写在这里的目的也是希望能给读者一个直观的感受。

三、pthread

pthread这个方案在这里只为大家了解,可能很多linux开发人员在使用这种方案进行多线程开发操作。不过对于我来讲在ios开发中我一次都没用过。


使用pthread 要引入头文件

#import <pthread.h>

然后创建线程

pthread_t thread = NULL;

id str = @"i'm pthread param";

pthread_create(&thread, NULL, operate, (__bridge void *)(str));

pthread_create的函数原型为

int pthread_create(pthread_t * __restrict, const pthread_attr_t * __restrict,

  void *(*)(void *), void * __restrict);

第一个参数pthread_t * __restrict
由于c语言没有对象的概念,所以pthread_t实际是一个结构体
所以创建的thread是一个指向当前新建线程的指针

typedef __darwin_pthread_t pthread_t;

typedef struct _opaque_pthread_t *__darwin_pthread_t;

struct _opaque_pthread_t {

      long __sig;

      struct __darwin_pthread_handler_rec  *__cleanup_stack;

      char __opaque[__PTHREAD_SIZE__];

};

第二个参数const pthread_attr_t * __restrict
同样是一个结构体,这里是用来设置线程属性的

typedef __darwin_pthread_attr_t pthread_attr_t;

typedef struct _opaque_pthread_attr_t __darwin_pthread_attr_t;

struct _opaque_pthread_attr_t {

    long __sig;

    char __opaque[__PTHREAD_ATTR_SIZE__];

};

第三个参数void ()(void *)这里给出了一个函数指针,指向的是一个函数的起始地址,所以是线程开启后的回调函数,这里demo给出的是operate函数,在线程中进行耗时操作。
第四个参数是回调函数所用的参数

void *operate(void *param) {

    NSString *str = (__bridge NSString *)(param);

    // 用循环模拟耗时操作

    for (int i = 0; i < 100000; i++) {

        // [NSThread currentThread] 为获取当前

        NSLog(@"timeConsuming in %@, times: %d, param: %@", [NSThread currentThread], i, str);

    }

    pthread_exit((void*)0);

}

那么,Pthreads在这里就介绍这么多,要注意的一点是在使用Pthreads的时候一定要手动把当前线程结束掉。因为我们经常使用的GCD和NSOperation已经被苹果封装过了,所以我们想要定制进行线程的操作就会有一定限制,如果有想从底层进行定制的读者,可以去搜一下相关的资料。

四、 NSThread

NSThread由苹果进行了封装,并且完全面向对象。所以可以直接使用OC方法操控线程对象,非常直观和方便。可以说对于ios开发人员而言,使用NSThread就开始了真正的多线程开发。所以,通过NSThread我们具体讨论一些线程相关的问题,包括如下内容:

  • 使用NSThread创建线程

  • 线程状态

  • 线程间通信

  • 线程安全

1. 使用NSThread创建线程

使用NSThread创建线程有以下几种方式:

  1. 使用NSThread的init方法显式创建

  2. 使用NSThread类方法显式创建并启动线程

  3. 隐式创建并启动线程

具体的代码实现在下面已经给出了,这里提醒大家注意一点。只有使用NSThread的init方法创建的线程才会返回具体的线程实例。也就是说如果想要对线程做更多的控制,比如添加线程的名字、更改优先级等操作,要使用第一种方式来创建线程。但是此种方法需要使用start方法来手动启动线程。

/**

 *  隐式创建并启动线程

 */

- (void)createThreadWithImplicit {

    // 隐式创建并启动线程

    [self performSelectorInBackground:@selector(threadMethod3:) withObject:@"implicitMethod"];

}

/**

 *  使用NSThread类方法显式创建并启动线程

 */

- (void)createThreadWithClassMethod {

    // 使用类方法创建线程并自动启动线程

    [NSThread detachNewThreadSelector:@selector(threadMethod2:) toTarget:self withObject:@"fromClassMethod"];

}

/**

 *  使用init方法显式创建线程

 */

- (void)createThreadWithInit {

    // 创建线程

    NSThread *thread1 = [[NSThread alloc] initWithTarget:self selector:@selector(threadMethod1) object:nil];

    // 设置线程名

    [thread1 setName:@"thread1"];

    // 设置优先级 优先级从0到1 1最高

    [thread1 setThreadPriority:0.9];

    // 启动线程

    [thread1 start];

}

2. 线程状态

线程状态分为:启动线程, 阻塞线程结束线程
启动线程:

// 线程启动

- (void)start;

阻塞线程:

// 线程休眠到某一时刻+ (void)sleepUntilDate:(NSDate *)date;// 线程休眠多久+ (void)sleepForTimeInterval:(NSTimeInterval)ti;

结束线程:

// 结束线程

+ (void)exit;

大家在看官方api的时候可能会有一个疑问,api里明明有cancel方法,为什么使用cancel方法不能结束线程?
当我们使用cancel方法时,只是改变了线程的状态标识,并不能结束线程,所以我们要配合isCancelled方法进行使用。具体实现如下:

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    // 创建线程

    [self createThread];

}

/**

 *  创建线程

 */

- (void)createThread {

    // 创建线程

    NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(threadMethod) object:nil];

    thread.name = @"i'm a new thread";

    // 启动线程

    [thread start];

}

/**

 *  线程方法

 */

- (void)threadMethod {

    NSLog(@"thread is create -- the name is: "%@"", [NSThread currentThread].name);

    // 线程阻塞 -- 延迟到某一时刻 --- 这里的时刻是3秒以后

    [NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:3]];

    NSLog(@"sleep end");

    NSLog(@"sleep again");

    // 线程阻塞 -- 延迟多久 -- 这里延迟2秒

    [NSThread sleepForTimeInterval:2];

    NSLog(@"sleep again end");

    for (int i = 0 ; i < 100; i++) {

        NSLog(@"thread working");

        if(30 == i) {

            NSLog(@"thread will dead");

            [[NSThread currentThread] cancel];

        }

        if([[NSThread currentThread] isCancelled]) {

            // 结束线程

//            [NSThread exit];

            return;

        }

    }

}

3. 线程间通信

线程间通信我们最常用的就是开启子线程进行耗时操作,操作完毕后回到主线程,进行数据赋值以及刷新主线程UI。在这里,用一个经典的图片下载demo进行简述。
首先我们先了解一下api给出的线程间通信的方法:

//与主线程通信

- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait modes:(NSArray *)array;

- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait;

  // equivalent to the first method with kCFRunLoopCommonModes

//与其他子线程通信

- (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(id)arg waitUntilDone:(BOOL)wait modes:(NSArray *)array NS_AVAILABLE(10_5, 2_0);

- (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(id)arg waitUntilDone:(BOOL)wait

以下是demo中的代码片段:

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    // 下载图片

    [self downloadImage];

}

/**

 *  下载图片

 */

- (void)downloadImage {

    // 创建线程下载图片

    [NSThread detachNewThreadSelector:@selector(downloadImageInThread) toTarget:self withObject:nil];

}

/**

 *  线程中下载图片操作

 */

- (void)downloadImageInThread {

    NSLog(@"come in sub thread -- %@", [NSThread currentThread]);

    // 获取图片url

    NSURL *url = [NSURL URLWithString:@"http://img.ycwb.com/news/attachement/jpg/site2/20110226/90fba60155890ed3082500.jpg"];

    // 计算耗时

    NSDate *begin = [NSDate date];

    // 使用CoreFoundation计算耗时 CFDate

    CFTimeInterval beginInCF = CFAbsoluteTimeGetCurrent();

    // 从url读取数据(下载图片) -- 耗时操作

    NSData *imageData = [NSData dataWithContentsOfURL:url];

    NSDate *end = [NSDate date];

    CFTimeInterval endInCF= CFAbsoluteTimeGetCurrent();

    // 计算时间差

    NSLog(@"time difference -- %f", [end timeIntervalSinceDate:begin]);

    NSLog(@"time difference inCF -- %f", endInCF - beginInCF);

    // 通过二进制data创建image

    UIImage *image = [UIImage imageWithData:imageData];

    // 回到主线程进行图片赋值和界面刷新

    [self performSelectorOnMainThread:@selector(backToMainThread:) withObject:image waitUntilDone:YES];

    // 这里也可以使用imageView的set方法进行操作

//    [self.imageView performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:YES];

}

/**

 *  回到主线程的操作

 */

- (void)backToMainThread:(UIImage *)image {

    NSLog(@"back to main thread --- %@", [NSThread currentThread]);

    // 赋值图片到imageview

    self.imageView.image = image;

在demo中已经把注释写的比较清晰了,需要补充的有三点:

  1. performSelectorOnMainThread:withObject:waitUntilDone:方法这里是回到了主线程进行操作,同样也可以使用

[self performSelector:@selector(backToMainThread:) onThread:[NSThread mainThread] withObject:image waitUntilDone:YES];

  1. 回到主线程,或者进入其他线程进行操作。

  2. 在实际项目中我们可能会分析耗时操作所花费时间或者分析用户行为的时候要计算用户在当前页面所耗时间,所以在demo中加入了时间的两种计算方式,分别是CoreFoundation和Foundation中的。

// 计算耗时

 NSDate *begin = [NSDate date];

 // 使用CoreFoundation计算耗时 CFDate

 CFTimeInterval beginInCF = CFAbsoluteTimeGetCurrent();

 // 从url读取数据(下载图片) -- 耗时操作

 NSData *imageData = [NSData dataWithContentsOfURL:url];

 NSDate *end = [NSDate date];

 CFTimeInterval endInCF= CFAbsoluteTimeGetCurrent();

 // 计算时间差

 NSLog(@"time difference -- %f", [end timeIntervalSinceDate:begin]);

 NSLog(@"time difference inCF -- %f", endInCF - beginInCF);

3. 如果自己写的项目无法运行,可能是因为Xcode7 创建HTTP请求报错导致,具体解决方案请点击这里

4. 线程安全

因为是多线程操作,所以会存在一定的安全隐患。原因是多线程会存在不同线程的资源共享,也就是说我们可能在同一时刻两个线程同时操作了某一个变量的值,但是线程的对变量的操作不同,导致变量的值出现误差。下面是一个存取钱的demo片段:

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    // 初始化状态

    [self initStatus];

}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    // 启动线程

    [self startThread];

}

/**

 *  初始化状态

 */

- (void)initStatus {

    // 设置存款

    self.depositMoney = 5000;

    // 创建存取钱线程

    self.saveThread = [[NSThread alloc] initWithTarget:self selector:@selector(saveAndDraw) object:nil];

    self.saveThread.name = @"save";

    self.drawThread = [[NSThread alloc] initWithTarget:self selector:@selector(saveAndDraw) object:nil];

    self.drawThread.name = @"draw";

}

/**

 *  开启线程

 */

- (void)startThread {

    // 开启存取钱线程

    [self.saveThread start];

    [self.drawThread start];

}

/**

 *  存取钱操作

 */

- (void)saveAndDraw {

    while(1) {

        if(self.depositMoney > 3000) {

            // 阻塞线程,模拟操作花费时间

            [NSThread sleepForTimeInterval:0.05];

            if([[NSThread currentThread].name isEqualToString:@"save"]) {

                self.depositMoney += 100;

            } else {

                self.depositMoney -= 100;

            }

            NSLog(@"currentThread: %@, depositMoney: %d", [NSThread currentThread].name, self.depositMoney);

        } else {

            NSLog(@"no money");

            return;

        }

    }

}

在上面的demo中我们发现,存取钱的线程是同时开启的,而存取钱的钱数相同,所以每一次存取操作结束后,存款值应该不会改变。大家可以运行demo进行查看结果。
所以需要在线程操作中加入锁:

/**

 *  存取钱操作

 */

- (void)saveAndDraw {

    while(1) {

        // 互斥锁

        @synchronized (self) {

            if(self.depositMoney > 3000) {

               // 阻塞线程,模拟操作花费时间

                [NSThread sleepForTimeInterval:0.05];

                if([[NSThread currentThread].name isEqualToString:@"save"]) {

                    self.depositMoney += 100;

                } else {

                    self.depositMoney -= 100;

                }

                NSLog(@"currentThread: %@, depositMoney: %d", [NSThread currentThread].name, self.depositMoney);

            } else {

                NSLog(@"no money");

                return;

            }

        }

    }

}

线程安全解决方案:

* 互斥锁

@synchronized 的作用是创建一个互斥锁,保证此时没有其它线程对锁住的对象进行修改。

* 互斥锁使用格式:

@synchronized (锁对象) { // 需要锁定的代码 }

* 互斥锁的优缺点:

优点: 防止多线程对共享资源进行抢夺造成的数据安全问题

缺点: 需要消耗大量cpu资源

注:NSThread头文件中的相关方法

//获取当前线程

 +(NSThread *)currentThread; 

//创建线程后自动启动线程

+ (void)detachNewThreadSelector:(SEL)selector toTarget:(id)target withObject:(id)argument;

//是否是多线程

+ (BOOL)isMultiThreaded;

//线程字典

- (NSMutableDictionary *)threadDictionary;

//线程休眠到什么时间

+ (void)sleepUntilDate:(NSDate *)date;

//线程休眠多久

+ (void)sleepForTimeInterval:(NSTimeInterval)ti;

//退出线程

+ (void)exit;

//线程优先级

+ (double)threadPriority;

+ (BOOL)setThreadPriority:(double)p;

- (double)threadPriority NS_AVAILABLE(10_6, 4_0);

- (void)setThreadPriority:(double)p NS_AVAILABLE(10_6, 4_0);

//调用栈返回地址

+ (NSArray *)callStackReturnAddresses NS_AVAILABLE(10_5, 2_0);

+ (NSArray *)callStackSymbols NS_AVAILABLE(10_6, 4_0);

//设置线程名字

- (void)setName:(NSString *)n NS_AVAILABLE(10_5, 2_0);

- (NSString *)name NS_AVAILABLE(10_5, 2_0);

//获取栈的大小

- (NSUInteger)stackSize NS_AVAILABLE(10_5, 2_0);

- (void)setStackSize:(NSUInteger)s NS_AVAILABLE(10_5, 2_0);

//是否是主线程

- (BOOL)isMainThread NS_AVAILABLE(10_5, 2_0);

+ (BOOL)isMainThread NS_AVAILABLE(10_5, 2_0); // reports whether current thread is main

+ (NSThread *)mainThread NS_AVAILABLE(10_5, 2_0);

//初始化方法

- (id)init NS_AVAILABLE(10_5, 2_0); // designated initializer

- (id)initWithTarget:(id)target selector:(SEL)selector object:(id)argument NS_AVAILABLE(10_5, 2_0);

//是否正在执行

- (BOOL)isExecuting NS_AVAILABLE(10_5, 2_0);

//是否执行完成

- (BOOL)isFinished NS_AVAILABLE(10_5, 2_0);

//是否取消线程

- (BOOL)isCancelled NS_AVAILABLE(10_5, 2_0);

- (void)cancel NS_AVAILABLE(10_5, 2_0);

//线程启动

- (void)start NS_AVAILABLE(10_5, 2_0);

- (void)main NS_AVAILABLE(10_5, 2_0); // thread body method

@end

//多线程通知

FOUNDATION_EXPORT NSString * const NSWillBecomeMultiThreadedNotification;

FOUNDATION_EXPORT NSString * const NSDidBecomeSingleThreadedNotification;

FOUNDATION_EXPORT NSString * const NSThreadWillExitNotification;

@interface NSObject (NSThreadPerformAdditions)

//与主线程通信

- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait modes:(NSArray *)array;

- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait;

  // equivalent to the first method with kCFRunLoopCommonModes

//与其他子线程通信

- (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(id)arg waitUntilDone:(BOOL)wait modes:(NSArray *)array NS_AVAILABLE(10_5, 2_0);

- (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(id)arg waitUntilDone:(BOOL)wait NS_AVAILABLE(10_5, 2_0);

  // equivalent to the first method with kCFRunLoopCommonModes

//隐式创建并启动线程

- (void)performSelectorInBackground:(SEL)aSelector withObject:(id)arg NS_AVAILABLE(10_5, 2_0);

由于多线程内容比较多,所以这里拆成两个部分。此文介绍PThread和NSThread,下篇文章将会跟大家一起讨论一下GCD和NSOperation的知识。希望本文章能对大家有所帮助。

原文地址:https://www.cnblogs.com/fengmin/p/5548399.html