iOS有三种多线程编程的技术

1、简介:

1.1 iOS有三种多线程编程的技术,分别是:

1.、NSThread 

2、Cocoa NSOperation (iOS多线程编程之NSOperation和NSOperationQueue的使用

3、GCD  全称:Grand Central Dispatch( iOS多线程编程之Grand Central Dispatch(GCD)介绍和使用

这三种编程方式从上到下,抽象度层次是从低到高的,抽象度越高的使用越简单,也是Apple最推荐使用的。

这篇我们主要介绍和使用NSThread,后面会继续2、3 的讲解和使用。

1.2 三种方式的有缺点介绍:

NSThread:

优点:NSThread 比其他两个轻量级

缺点:需要自己管理线程的生命周期,线程同步。线程同步对数据的加锁会有一定的系统开销

NSThread实现的技术有下面三种:


Technology

Description

Cocoa threads

Cocoa implements threads using the NSThread class. Cocoa also provides methods on NSObject for spawning new threads and executing code on already-running threads. For more information, see “Using NSThread” and “Using NSObject to Spawn a Thread.”

POSIX threads

POSIX threads provide a C-based interface for creating threads. If you are not writing a Cocoa application, this is the best choice for creating threads. The POSIX interface is relatively simple to use and offers ample flexibility for configuring your threads. For more information, see “Using POSIX Threads”

Multiprocessing Services

Multiprocessing Services is a legacy C-based interface used by applications transitioning from older versions of Mac OS. This technology is available in OS X only and should be avoided for any new development. Instead, you should use the NSThread class or POSIX threads. If you need more information on this technology, see Multiprocessing Services Programming Guide.

一般使用cocoa thread 技术。



Cocoa operation 

优点:不需要关心线程管理,数据同步的事情,可以把精力放在自己需要执行的操作上。

Cocoa operation 相关的类是 NSOperation ,NSOperationQueue。NSOperation是个抽象类,使用它必须用它的子类,可以实现它或者使用它定义好的两个子类:NSInvocationOperation 和 NSBlockOperation。创建NSOperation子类的对象,把对象添加到NSOperationQueue队列里执行。

GCD

Grand Central Dispatch (GCD)是Apple开发的一个多核编程的解决方法。在iOS4.0开始之后才能使用。GCD是一个替代诸如NSThread, NSOperationQueue, NSInvocationOperation等技术的很高效和强大的技术。现在的iOS系统都升级到6了,所以不用担心该技术不能使用。


介绍完这三种多线程编程方式,我们这篇先介绍NSThread的使用。

2、NSThread的使用

2.1 NSThread 有两种直接创建方式:

- (id)initWithTarget:(id)target selector:(SEL)selector object:(id)argument

+ (void)detachNewThreadSelector:(SEL)aSelector toTarget:(id)aTarget withObject:(id)anArgument

第一个是实例方法,第二个是类方法


[cpp] view plain copy


  1. 1、[NSThread detachNewThreadSelector:@selector(doSomething:) toTarget:self withObject:nil];  
  2. 2、NSThread* myThread = [[NSThread alloc] initWithTarget:self  
  3.                                         selector:@selector(doSomething:)  
  4.                                         object:nil];  
  5. [myThread start];  

2.2参数的意义:


selector :线程执行的方法,这个selector只能有一个参数,而且不能有返回值。

target  :selector消息发送的对象

argument:传输给target的唯一参数,也可以是nil

第一种方式会直接创建线程并且开始运行线程,第二种方式是先创建线程对象,然后再运行线程操作,在运行线程操作前可以设置线程的优先级等线程信息

2.3 PS:不显式创建线程的方法:

用NSObject的类方法  performSelectorInBackground:withObject: 创建一个线程:

[Obj performSelectorInBackground:@selector(doSomething) withObject:nil];

2.4 下载图片的例子:

2.4.1  新建singeView app

新建项目,并在xib文件上放置一个imageView控件。按住control键拖到viewControll

er.h文件中创建imageView IBOutlet 

ViewController.m中实现:

[cpp] view plain copy


  1. //  
  2. //  ViewController.m  
  3. //  NSThreadDemo  
  4. //  
  5. //  Created by rongfzh on 12-9-23.  
  6. //  Copyright (c) 2012年 rongfzh. All rights reserved.  
  7. //  
  8.   
  9. #import "ViewController.h"  
  10. #define kURL @"http://avatar.csdn.net/2/C/D/1_totogo2010.jpg"  
  11. @interface ViewController ()  
  12.   
  13. @end  
  14.   
  15. @implementation ViewController  
  16.   
  17. -(void)downloadImage:(NSString *) url{  
  18.     NSData *data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:url]];  
  19.     UIImage *image = [[UIImage alloc]initWithData:data];  
  20.     if(image == nil){  
  21.           
  22.     }else{  
  23.         [self performSelectorOnMainThread:@selector(updateUI:) withObject:image waitUntilDone:YES];  
  24.     }  
  25. }  
  26.   
  27. -(void)updateUI:(UIImage*) image{  
  28.     self.imageView.image = image;  
  29. }  
  30.   
  31.   
  32. - (void)viewDidLoad  
  33. {  
  34.     [super viewDidLoad];  
  35.       
  36. //    [NSThread detachNewThreadSelector:@selector(downloadImage:) toTarget:self withObject:kURL];  
  37.     NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(downloadImage:) object:kURL];  
  38.     [thread start];  
  39. }  
  40.   
  41. - (void)didReceiveMemoryWarning  
  42. {  
  43.     [super didReceiveMemoryWarning];  
  44.     // Dispose of any resources that can be recreated.  
  45. }  
  46.   
  47. @end  

2.4.2线程间通讯

线程下载完图片后怎么通知主线程更新界面呢?


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

performSelectorOnMainThread是NSObject的方法,除了可以更新主线程的数据外,还可以更新其他线程的比如:

用:performSelector:onThread:withObject:waitUntilDone: 

运行下载图片:


图片下载下来了。

2.3 线程同步

我们演示一个经典的卖票的例子来讲NSThread的线程同步:

.h


[cpp] view plain copy


  1. #import <UIKit/UIKit.h>  
  2.   
  3. @class ViewController;  
  4.   
  5. @interface AppDelegate : UIResponder <UIApplicationDelegate>  
  6. {  
  7.     int tickets;  
  8.     int count;  
  9.     NSThread* ticketsThreadone;  
  10.     NSThread* ticketsThreadtwo;  
  11.     NSCondition* ticketsCondition;  
  12.     NSLock *theLock;  
  13. }  
  14. @property (strong, nonatomic) UIWindow *window;  
  15.   
  16. @property (strong, nonatomic) ViewController *viewController;  
  17.   
  18. @end  



[cpp] view plain copy


  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  
  2. {  
  3.       
  4.     tickets = 100;  
  5.     count = 0;  
  6.     theLock = [[NSLock alloc] init];  
  7.     // 锁对象  
  8.     ticketsCondition = [[NSCondition alloc] init];  
  9.     ticketsThreadone = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];  
  10.     [ticketsThreadone setName:@"Thread-1"];  
  11.     [ticketsThreadone start];  
  12.       
  13.       
  14.     ticketsThreadtwo = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];  
  15.     [ticketsThreadtwo setName:@"Thread-2"];  
  16.     [ticketsThreadtwo start];  
  17.       
  18.     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];  
  19.     // Override point for customization after application launch.  
  20.     self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];  
  21.     self.window.rootViewController = self.viewController;  
  22.     [self.window makeKeyAndVisible];  
  23.     return YES;  
  24. }  
  25.   
  26. - (void)run{  
  27.     while (TRUE) {  
  28.         // 上锁  
  29. //        [ticketsCondition lock];  
  30.         [theLock lock];  
  31.         if(tickets >= 0){  
  32.             [NSThread sleepForTimeInterval:0.09];  
  33.             count = 100 - tickets;  
  34.             NSLog(@"当前票数是:%d,售出:%d,线程名:%@",tickets,count,[[NSThread currentThread] name]);  
  35.             tickets--;  
  36.         }else{  
  37.             break;  
  38.         }  
  39.         [theLock unlock];  
  40. //        [ticketsCondition unlock];  
  41.     }  
  42. }  

如果没有线程同步的lock,卖票数可能是-1.加上lock之后线程同步保证了数据的正确性。

上面例子我使用了两种锁,一种NSCondition ,一种是:NSLock。 NSCondition我已经注释了。


线程的顺序执行

他们都可以通过

        [ticketsCondition signal]; 发送信号的方式,在一个线程唤醒另外一个线程的等待。

比如:


[cpp] view plain copy


  1. #import "AppDelegate.h"  
  2.   
  3. #import "ViewController.h"  
  4.   
  5. @implementation AppDelegate  
  6.   
  7. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  
  8. {  
  9.       
  10.     tickets = 100;  
  11.     count = 0;  
  12.     theLock = [[NSLock alloc] init];  
  13.     // 锁对象  
  14.     ticketsCondition = [[NSCondition alloc] init];  
  15.     ticketsThreadone = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];  
  16.     [ticketsThreadone setName:@"Thread-1"];  
  17.     [ticketsThreadone start];  
  18.       
  19.     ticketsThreadtwo = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];  
  20.     [ticketsThreadtwo setName:@"Thread-2"];  
  21.     [ticketsThreadtwo start];  
  22.       
  23.     NSThread *ticketsThreadthree = [[NSThread alloc] initWithTarget:self selector:@selector(run3) object:nil];  
  24.     [ticketsThreadthree setName:@"Thread-3"];  
  25.     [ticketsThreadthree start];      
  26.     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];  
  27.     // Override point for customization after application launch.  
  28.     self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];  
  29.     self.window.rootViewController = self.viewController;  
  30.     [self.window makeKeyAndVisible];  
  31.     return YES;  
  32. }  
  33.   
  34. -(void)run3{  
  35.     while (YES) {  
  36.         [ticketsCondition lock];  
  37.         [NSThread sleepForTimeInterval:3];  
  38.         [ticketsCondition signal];  
  39.         [ticketsCondition unlock];  
  40.     }  
  41. }  
  42.   
  43. - (void)run{  
  44.     while (TRUE) {  
  45.         // 上锁  
  46.         [ticketsCondition lock];  
  47.         [ticketsCondition wait];  
  48.         [theLock lock];  
  49.         if(tickets >= 0){  
  50.             [NSThread sleepForTimeInterval:0.09];  
  51.             count = 100 - tickets;  
  52.             NSLog(@"当前票数是:%d,售出:%d,线程名:%@",tickets,count,[[NSThread currentThread] name]);  
  53.             tickets--;  
  54.         }else{  
  55.             break;  
  56.         }  
  57.         [theLock unlock];  
  58.         [ticketsCondition unlock];  
  59.     }  
  60. }  

wait是等待,我加了一个 线程3 去唤醒其他两个线程锁中的wait


其他同步

我们可以使用指令 @synchronized 来简化 NSLock的使用,这样我们就不必显示编写创建NSLock,加锁并解锁相关代码。

- (void)doSomeThing:(id)anObj

{

    @synchronized(anObj)

    {

        // Everything between the braces is protected by the @synchronized directive.

    }

}

还有其他的一些锁对象,比如:循环锁NSRecursiveLock,条件锁NSConditionLock,分布式锁NSDistributedLock等等,可以自己看官方文档学习

NSThread下载图片的例子代码:http://download.csdn.net/detail/totogo2010/4591149

著作权声明:本文由http://blog.csdn.net/totogo2010/原创,欢迎转载分享。请尊重作者劳动,转载时保留该声明和作者博客链接,谢谢!




从上一篇的内容我们知道,在iOS中一个事件用一个UIEvent对象表示,UITouch用来表示一次对屏幕的操作动作,由多个UITouch对象构成了一个UIEvent对象。另外,UIResponder是所有响应者的父类,UIView、UIViewController、UIWindow、UIApplication都直接或间接的集成了UIResponder。关于事件响应者链的传递机制在上一篇中也有阐述,如果你还不是很了解,可以先看看iOS事件机制(一)

事件处理方法

UIResponder中定义了一系列对事件的处理方法,他们分别是:

  • –(void)touchesBegan:(NSSet )touches withEvent:(UIEvent )event
  • –(void)touchesMoved:(NSSet )touches withEvent:(UIEvent )event
  • –(void)touchesEnded:(NSSet )touches withEvent:(UIEvent )event
  • –(void)touchesCancelled:(NSSet )touches withEvent:(UIEvent )event

从方法名字可以知道,他们分别对应了屏幕事件的开始、移动、结束和取消几个阶段,前三个阶段理解都没问题,最后一个取消事件的触发时机是在诸如突然来电话或是系统杀进程时调用。这些方法的第一个参数定义了UITouch对象的一个集合(NSSet),它的数量表示了这次事件是几个手指的操作,目前iOS设备支持的多点操作手指数最多是5。第二个参数是当前的UIEvent对象。下图展示了一个UIEvent对象与多个UITouch对象之间的关系。


一、点击事件

首先,新建一个自定义的View继承于UIView,并实现上述提到的事件处理方法,我们可以通过判断UITouch的tapCount属性来决定响应单击、双击或是多次点击事件。

MyView.m

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

#import "MyView.h"

@implementation MyView

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

{

}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

{

    for (UITouch *aTouch in touches) {

        if (aTouch.tapCount == 2) {

            // 处理双击事件

            [self respondToDoubleTapGesture];

        }

    }

}

-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event

{

}

-(void)respondToDoubleTapGesture

{

    NSLog(@"respondToDoubleTapGesture");

}

@end

二、滑动事件

滑动事件一般包括上下滑动和左右滑动,判断是否是一次成功的滑动事件需要考虑一些问题,比如大部分情况下,用户进行一次滑动操作,这次滑动是否是在一条直线上?或者是否是基本能保持一条直线的滑动轨迹。或者判断是上下滑动还是左右滑动等。另外,滑动手势一般有一个起点和一个终点,期间是在屏幕上画出的一个轨迹,所以需要对这两个点进行判断。我们修改上述的MyView.m的代码来实现一次左右滑动的事件响应操作。

MyView.m

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

#import "MyView.h"

#define HORIZ_SWIPE_DRAG_MIN  12    //水平滑动最小间距

#define VERT_SWIPE_DRAG_MAX    4    //垂直方向最大偏移量

@implementation MyView

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    UITouch *aTouch = [touches anyObject];

    // startTouchPosition是一个CGPoint类型的属性,用来存储当前touch事件的位置

    self.startTouchPosition = [aTouch locationInView:self];

}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

{

}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

{

    UITouch *aTouch = [touches anyObject];

    CGPoint currentTouchPosition = [aTouch locationInView:self];

    //  判断水平滑动的距离是否达到了设置的最小距离,并且是否是在接近直线的路线上滑动(y轴偏移量)

    if (fabsf(self.startTouchPosition.x - currentTouchPosition.x) >= HORIZ_SWIPE_DRAG_MIN &&

        fabsf(self.startTouchPosition.y - currentTouchPosition.y) <= VERT_SWIPE_DRAG_MAX)

    {

        // 满足if条件则认为是一次成功的滑动事件,根据x坐标变化判断是左滑还是右滑

        if (self.startTouchPosition.x < currentTouchPosition.x) {

            [self rightSwipe];//右滑响应方法

        } else {

            [self leftSwipe];//左滑响应方法

        }

        //重置开始点坐标值

        self.startTouchPosition = CGPointZero;

    }

}

-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event

{

  //当事件因某些原因取消时,重置开始点坐标值

    self.startTouchPosition = CGPointZero;

}

-(void)rightSwipe

{

    NSLog(@"rightSwipe");

}

-(void)leftSwipe

{

    NSLog(@"leftSwipe");

}

@end

三、拖拽事件

在屏幕上我们可以拖动某一个控件(View)进行移动,这种事件成为拖拽事件,其实现原理就是在不改变View的大小尺寸的前提下改变View的显示坐标值,为了达到动态移动的效果,我们可以在move阶段的方法中进行坐标值的动态更改,还是重写MyView.m的事件处理方法,这次在touchesMove方法中进行处理。

MyView.m

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

#import "MyView.h"

@implementation MyView

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

{

    UITouch *aTouch = [touches anyObject];

    //获取当前触摸操作的位置坐标

    CGPoint loc = [aTouch locationInView:self];

    //获取上一个触摸点的位置坐标

    CGPoint prevloc = [aTouch previousLocationInView:self];

    CGRect myFrame = self.frame;

    //改变View的x、y坐标值

    float deltaX = loc.x - prevloc.x;

    float deltaY = loc.y - prevloc.y;

    myFrame.origin.x += deltaX;

    myFrame.origin.y += deltaY;

    //重新设置View的显示位置

    [self setFrame:myFrame];

}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

{

}

-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event

{

}

@end

四、双指缩放

之前提到过UIEvent包含了一系列的UITouch对象构成一次事件,当设计多点触控操作时,可与对UIEvent对象内的UITouch对象进行处理,比如实现一个双指缩放的功能。

MyView.m

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

#import "MyView.h"

@implementation MyView

{

    BOOL pinchZoom;

    CGFloat previousDistance;

    CGFloat zoomFactor;

}

-(id)init

{

    self = [super init];

    if (self) {

        pinchZoom = NO;

        //缩放前两个触摸点间的距离

        previousDistance = 0.0f;

        zoomFactor = 1.0f;

    }

    return self;

}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    if(event.allTouches.count == 2) {

        pinchZoom = YES;

        NSArray *touches = [event.allTouches allObjects];

        //接收两个手指的触摸操作

        CGPoint pointOne = [[touches objectAtIndex:0] locationInView:self];

        CGPoint pointTwo = [[touches objectAtIndex:1] locationInView:self];

        //计算出缩放前后两个手指间的距离绝对值(勾股定理)

        previousDistance = sqrt(pow(pointOne.x - pointTwo.x, 2.0f) +

                                pow(pointOne.y - pointTwo.y, 2.0f));

    } else {

        pinchZoom = NO;

    }

}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

{

    if(YES == pinchZoom && event.allTouches.count == 2) {

        NSArray *touches = [event.allTouches allObjects];

        CGPoint pointOne = [[touches objectAtIndex:0] locationInView:self];

        CGPoint pointTwo = [[touches objectAtIndex:1] locationInView:self];

        //两个手指移动过程中,两点之间距离

        CGFloat distance = sqrt(pow(pointOne.x - pointTwo.x, 2.0f) +

                                pow(pointOne.y - pointTwo.y, 2.0f));

        //换算出缩放比例

        zoomFactor += (distance - previousDistance) / previousDistance;

        zoomFactor = fabs(zoomFactor);

        previousDistance = distance;

        //缩放

        self.layer.transform = CATransform3DMakeScale(zoomFactor, zoomFactor, 1.0f);

    }

}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

{

    if(event.allTouches.count != 2) {

        pinchZoom = NO;

        previousDistance = 0.0f;

    }

}

-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event

{

}

@end

上面实现的方式有一点不足之处就是必须两个手指同时触摸按下才能达到缩放的效果,并不能达到相册里面那样一个手指触摸后,另一个手指按下也可以缩放。如果需要达到和相册照片缩放的效果,需要同时控制begin、move、end几个阶段的事件处理。这个不足就留给感兴趣的同学自己去实现了。


Dispatch Queue

Dispatch Queue是一个任务执行队列,可以让你异步或同步地执行多个Block或函数。Dispatch Queue是FIFO的,即先入队的任务总会先执行。目前有三种类型的Dispath Queue:

  • 串行队列(Serial dispatch queue)
  • 并发队列(Concurrent dispatch queue)
  • 主队列(Main dispatch queue)

串行队列

串行队列一次只能处理一个任务,可以由用户调用dispatch_queue_create创建:

dispatch_queue_t queue;

queue = dispatch_queue_create("com.example.MyQueue", NULL);

dispatch_queue_create第一个参数是串行队列标识,一般用反转域名的格式表示以防冲突;第二个参数是queue的类型,设为NULL时默认是DISPATCH_QUEUE_SERIAL,将创建串行队列,在必要情况下,你可以将其设置为DISPATCH_QUEUE_CONCURRENT来创建自定义并行队列。

并行队列

并行队列可以同时处理多个任务,在不得以的情况下可以用dispatch_queue_create创建,但一般我们都要用系统预定义的并行队列,即全局队列(Global Concurrent Dispatch Queues)。目前系统预定义了四个不同运行优先级的全局队列,我们可以通过dispatch_get_global_queue来获取它们。

dispatch_queue_t aQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

dispatch_get_global_queue第一个参数是队列的优先级,分别对应四个全局队列:

  • DISPATCH_QUEUE_PRIORITY_HIGH
  • DISPATCH_QUEUE_PRIORITY_DEFAULT
  • DISPATCH_QUEUE_PRIORITY_LOW
  • DISPATCH_QUEUE_PRIORITY_BACKGROUND

dispatch_get_global_queue中第二个参数目前系统保留,请设置为0即可。

主队列

主队列是一个特殊的队列,它是系统预定义的运行在主线程的一个Dispatch Queue。可以通过dispatch_get_main_queue来获取唯一的主队列。主队列一般运行一些需要与主线程同步的一些短时任务。

dispatch_queue_t mainQueue = dispatch_get_main_queue();

获取当前队列

你可以通过dispatch_get_current_queue获取运行时的队列:

dispatch_queue_t currentQueue = dispatch_get_current_queue();

如果在队列执行任务中调用,返回执行此任务的队列;如果在主线程中调用,将返回主队列;如果在一般线程(非主线程线程非队列执行任务)中调用,返回DISPATCH_QUEUE_PRIORITY_DEFAULT全局队列。

在队列中运行任务

你可以随时向一个队列中添加一个新任务,只需要调用一下dispatch_async即可:

dispatch_async(aQueue, ^{

    //Do some work;

});

dispatch_async中的任务是异步执行的,就是说dispatch_async添加任务到执行队列后会立刻返回,而不会等待任务执行完成。然而,必要的话,你也可以调用dispatch_sync来同步的执行一个任务:

dispatch_sync(aQueue, ^{

    //Do some work;

});

dispatch_sync会阻塞当前线程直到提交的任务完全执行完毕。

Dispatch Queue的内存管理

除了系统预定义的Dispatch Queue,我们自定义的Dispatch Queue需要手动的管理它的内存。dispatch_retaindispatch_release这两个函数可以控制Dispatch Queue的引用计数(同时可以控制后面会讲到的Dispatch Group和Dispatch Source的引用计数)。当Dispatch Queue引用计数变为0后,就会调用finalizer,finalizer是Dispatch Queue销毁前调用的函数,用来清理Dispatch Queue的相关资源。可以用dispatch_set_finalizer_f函数来设置Dispatch Queue的finalizer,这个函数同时可以设置Dispatch Group和Dispatch Source的销毁函数(后面会讲到)。

void dispatch_set_finalizer_f(dispatch_object_t object, dispatch_function_t finalizer);

Dispatch Queue的上下文环境数据

我们可以为每个Dispatch Queue设置一个自定义的上下文环境数据,调用dispatch_set_context来实现。同时我们也可以用dispatch_get_context获取这个上下文环境数据,这个函数同时可以设置Dispatch Group和Dispatch Source的上下文环境数据(后面会讲到)。

void dispatch_set_context(dispatch_object_t object,void *context);

void * dispatch_get_context(dispatch_object_t object);

注意Dispatch Queue并不保证这个context不会释放,不会对它进行内存管理控制。我们需要自行管理context的内存分配和释放。一般我们非配内存设置context后,可以在finalizer里释放context占有的内存。

并行执行循环

在编程过程中,我们经常会用到for循环,而且for循环要做很多相关的任务。比如:

for (i = 0; i < count; i++) {

   //do a lot of work here.

   doSomething(i);

}

如果for循环中处理的任务是可并发的,显然放到一个线程中处理是很慢的,GCD提供两个函数dispatch_applydispatch_apply_fdispatch_apply是用于Block的,而dispatch_apply_f可以用于c函数,它们可以替代可并发的for循环,来并行的运行而提高执行效率。

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

dispatch_apply(count, queue, ^(size_t i) {

   //do a lot of work here.

   doSomething(i);

});

Dispatch Group

有时候我们进行下一步操作,而这个操作需要等待几个任务处理完毕后才能继续,这时我们就需要用的Dispatch Group(类似thread join)。我们可以把若干个任务放到一个Dispatch Group中:

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

dispatch_group_t group = dispatch_group_create();

dispatch_group_async(group, queue, ^{

   // Some asynchronous work

});

dispatch_group_asyncdispatch_async一样,会把任务放到queue中执行,不过它比dispatch_async多做了一步操作就是把这个任务和group相关联。

把一些任务放到Dispatch Group后,我们就可以调用dispatch_group_wait来等待这些任务完成。若任务已经全部完成或为空,则直接返回,否则等待所有任务完成后返回。注意:返回后group会清空。

dispatch_group_wait(group, DISPATCH_TIME_FOREVER);

// Do some work after.

dispatch_release(group);

Dispatch信号量

很多程序设计都设计到信号量,生产者-消费者模型在多线程编程中会频繁的使用。GCD提供了自己的一套信号量机制。

dispatch_semaphore_t sema = dispatch_semaphore_create(RESOURCE_SIZE);

dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);

//do some work here.

dispatch_semaphore_signal(sema);

dispatch_semaphore_wait用来获取信号量,若信号量为0,则等待直到信号量大于0。在处理任务结束后,应释放相关资源并调用dispatch_semaphore_signal使信号量增加1个。

Dispatch Source

Dispatch Source是GCD中监听一些系统事件的有个Dispatch对象,它包括定时器、文件监听、进程监听、Mach port监听等类型。

可以通过dispatch_source_create创建一个Dispatch Source:

dispatch_source_t dispatch_source_create(

   dispatch_source_type_t type,

   uintptr_t handle,

   unsigned long mask,

   dispatch_queue_t queue);

这里可以指定Dispatch Source的类型,type可以为文件读或写、进程监听等。handle为监听对象的句柄,如果是文件就是文件描述符,如果是进程就是进程ID。mask用来指定一些想要监听的事件,它的意义取决于typequeue指定事件处理的任务队列。

创建好Dispatch Source后,我们要为Dispatch Source设置一个事件处理模块。可以用dispatch_source_set_event_handlerdispatch_source_set_event_handler_f来设置:

void dispatch_source_set_event_handler(

   dispatch_source_t source,

   dispatch_block_t handler);

设置好Dispatch Source后就可以调用dispatch_resume来启动监听。如果相应的事件发生就会触发事件处理模块。

同时我们也可以设置一个取消处理模块:

dispatch_source_set_cancel_handler(mySource, ^{

   close(fd); // Close a file descriptor opened earlier.

});

取消处理模块会在Dispatch Source取消时调用。

下面介绍一下主要的Dispatch Source类型和示例代码。

定时器

定时器Dispatch Source可以每隔一个固定的时间处理一下任务。

dispatch_source_t CreateDispatchTimer(uint64_t interval,

              uint64_t leeway,

              dispatch_queue_t queue,

              dispatch_block_t block)

{

   dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER,

                                                     0, 0, queue);

   if (timer)

   {

      dispatch_source_set_timer(timer, dispatch_walltime(NULL, 0), interval, leeway);

      dispatch_source_set_event_handler(timer, block);

      dispatch_resume(timer);

   }

   return timer;

}


void MyCreateTimer()

{

   dispatch_source_t aTimer = CreateDispatchTimer(30ull * NSEC_PER_SEC,

                               1ull * NSEC_PER_SEC,

                               dispatch_get_main_queue(),

                               ^{ MyPeriodicTask(); });


   // Store it somewhere for later use.

    if (aTimer)

    {

        MyStoreTimer(aTimer);

    }

}

dispatch_after和dispatch_after_f

有时候我们只想处理一次延迟任务,可以用dispatch_after和dispatch_after_f

void dispatch_after(

   dispatch_time_t when,

   dispatch_queue_t queue,

   dispatch_block_t block);

监听文件事件

监听文件事件分好几个类型,有读、写、属性的监听。

读取文件

dispatch_source_t source = dispatch_source_create(DISPATCH_SOURCE_TYPE_READ, fd, 0, queue);

dispatch_source_set_event_handler(source, ^{

   // Get some data from the source variable, which is captured

   // from the parent context.

   size_t estimated = dispatch_source_get_data(source);

   // Continue reading the descriptor...

});

dispatch_resume(source);

写文件

dispatch_source_t writeSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_WRITE,

                        fd, 0, queue);

if (!writeSource)

{

    close(fd);

    return NULL;

}


dispatch_source_set_event_handler(writeSource, ^{

    size_t bufferSize = MyGetDataSize();

    void* buffer = malloc(bufferSize);


    size_t actual = MyGetData(buffer, bufferSize);

    write(fd, buffer, actual);


    free(buffer);


    // Cancel and release the dispatch source when done.

    dispatch_source_cancel(writeSource);

});

监听文件属性

dispatch_source_t source = dispatch_source_create(DISPATCH_SOURCE_TYPE_VNODE,

            fd, DISPATCH_VNODE_RENAME, queue);

if (source)

{

  // Copy the filename for later use.

  int length = strlen(filename);

  char* newString = (char*)malloc(length + 1);

  newString = strcpy(newString, filename);

  dispatch_set_context(source, newString);


  // Install the event handler to process the name change

  dispatch_source_set_event_handler(source, ^{

        const char*  oldFilename = (char*)dispatch_get_context(source);

        MyUpdateFileName(oldFilename, fd);

  });


  // Install a cancellation handler to free the descriptor

  // and the stored string.

  dispatch_source_set_cancel_handler(source, ^{

      char* fileStr = (char*)dispatch_get_context(source);

      free(fileStr);

      close(fd);

  });


  // Start processing events.

  dispatch_resume(source);

}

else

  close(fd);

监听进程事件

DISPATCH_PROC_EXIT是一个监听进程退出的类型。

dispatch_source_t source = dispatch_source_create(DISPATCH_SOURCE_TYPE_PROC,

                                                  parentPID, DISPATCH_PROC_EXIT, queue);

if (source)

{

   dispatch_source_set_event_handler(source, ^{

     MySetAppExitFlag();

     dispatch_source_cancel(source);

     dispatch_release(source);

   });

   dispatch_resume(source);

}

监听中断信号

dispatch_source_t source = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGHUP, 0, queue);

if (source)

{

  dispatch_source_set_event_handler(source, ^{

     MyProcessSIGHUP();

  });


  // Start processing signals

  dispatch_resume(source);

}

参考文献



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值