iOS多线程

多线程之NSInvocationOperation
多线程编程是防止主线程堵塞,增加运行效率等等的最佳方法。而原始的多线程方法存在很多的毛病,包括线程锁死等。在Cocoa中,Apple提供了NSOperation这个类,提供了一个优秀的多线程编程方法。
本次介绍NSOperation的子集,简易方法的NSInvocationOperation:
@implementation MyCustomClass
 - (void)launchTaskWithData:(id)data
{
    //创建一个NSInvocationOperation对象,并初始化到方法
    //在这里,selector参数后的值是你想在另外一个线程中运行的方法(函数,Method)
    //在这里,object后的值是想传递给前面方法的数据
    NSInvocationOperation* theOp = [[NSInvocationOperation alloc] initWithTarget:self
                    selector:@selector(myTaskMethod:) object:data];
 
    // 下面将我们建立的操作“Operation”加入到本地程序的共享队列中(加入后方法就会立刻被执行)
    // 更多的时候是由我们自己建立“操作”队列
    [[MyAppDelegate sharedOperationQueue] addOperation:theOp];
}
 
// 这个是真正运行在另外一个线程的“方法”
- (void)myTaskMethod:(id)data
{
    // Perform the task.
}
 
@end一个NSOperationQueue 操作队列,就相当于一个线程管理器,而非一个线程。因为你可以设置这个线程管理器内可以并行运行的的线程数量等等。下面是建立并初始化一个操作队列:

@interface MyViewController : UIViewController {
 
    NSOperationQueue *operationQueue;
    //在头文件中声明该队列
}
@end
 
@implementation MyViewController
 
- (id)init 
{
    self = [super init];
    if (self) {
        operationQueue = [[NSOperationQueue alloc] init]; //初始化操作队列
        [operationQueue setMaxConcurrentOperationCount:1];
        //在这里限定了该队列只同时运行一个线程
        //这个队列已经可以使用了
    }
    return self;
}
 
- (void)dealloc
{
    [operationQueue release];
    //正如Alan经常说的,我们是程序的好公民,需要释放内存!
    [super dealloc];
}
 
@end简单介绍之后,其实可以发现这种方法是非常简单的。很多的时候我们使用多线程仅仅是为了防止主线程堵塞,而NSInvocationOperation就是最简单的多线程编程,在iPhone编程中是经常被用到的。



///////////////////////////////////////////////////////////////////////////////////////////////////
1 在主线程里加入一个loading画面……
2 {
3 [window addSubview:view_loading];
4 [NSThread detachNewThreadSelector:@selector(init_backup:) toTarget:self withObject:nil];
5 }
可以通过performSelectorOhMainThread更新UI元素,比如设置进度条等等。最后消除loading画面,载入主View。
7 - (void)init_backup:(id)sender
8 {
9 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
10 
11 // ...
12 int i = status;
13 [self performSelectorOnMainThread:@selector(show_loading:) withObject:[NSNumber numberWithInt:i] waitUntil Done:NO];
14 
15 [view_loading removeFromSuperview];
16 [window addSubview:tabcontroller_main.view];
17 [pool release];
18 }

///////////////////////////////////////////////////////

利用iphone的多线程实现和线程同步

从接口的定义中可以知道,NSThread和大多数iphone的接口对象一样,有两种方式可以初始化:

一种使用initWithTarget :(id)target selector:(SEL)selector object:(id)argument,但需要负责在对象的retain count为0时调用对象的release方法清理对象。

另一种则使用所谓的convenient method,这个方便接口就是detachNewThreadSelector,这个方法可以直接生成一个线程并启动它,而且无需为线程的清理负责。

#import <UIKit/UIKit.h>
@interface SellTicketsAppDelegate : NSObject <UIApplicationDelegate> {
    int tickets;
    int count;
    NSThread* ticketsThreadone;
    NSThread* ticketsThreadtwo;
    NSCondition* ticketsCondition;
    UIWindow *window;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@end

然后在实现中添加如下代码:
//  SellTicketsAppDelegate.m
//  SellTickets
//
//  Created by sun dfsun2009 on 09-11-10.
//  Copyright __MyCompanyName__ 2009. All rights reserved.
//
#import "SellTicketsAppDelegate.h"
@implementation SellTicketsAppDelegate
@synthesize window;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
    tickets = 100;
    count = 0;
    // 锁对象
    ticketCondition = [[NSCondition alloc] init];
    ticketsThreadone = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];
    [ticketsThreadone setName:@"Thread-1"];
    [ticketsThreadone start];
    ticketsThreadtwo = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];
    [ticketsThreadtwo setName:@"Thread-2"];
    [ticketsThreadtwo start];
    //[NSThread detachNewThreadSelector:@selector(run) toTarget:self withObject:nil];
    // Override point for customization after application launch
    [window makeKeyAndVisible];
}
- (void)run{
    while (TRUE) {
        // 上锁
        [ticketsCondition lock];
        if(tickets > 0)
        {
            [NSThread sleepForTimeInterval:0.5];
            count = 100 - tickets;
            NSLog(@"当前票数是:%d,售出:%d,线程名:%@",tickets,count,[[NSThread currentThread] name]);
            tickets--;
        }else
        {
            break;
        }
        [ticketsCondition unlock];
    }
}
-         (void)dealloc {
    [ticketsThreadone release];
    [ticketsThreadtwo release];
    [ticketsCondition release];
    [window release];
    [super dealloc];
}
@end

-------------------------------------------------------------------------------------
// 定义
#import <UIKit/UIKit.h>

@interface ThreadSyncSampleViewController : UIViewController {
 int _threadCount;
 NSCondition *_myCondition;
}

@end

//实现文件如下:

#import "ThreadSyncSampleViewController.h"

@implementation ThreadSyncSampleViewController

/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
 //
 //_myCondition = nil;
 //
 _myCondition = [[NSCondition alloc] init];
 //
 NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:30 
             target:self
              selector:@selector(threadTester)
              userInfo:nil
             repeats:YES];
 [timer fire];
 
}


- (void)threadTester{
 [_myCondition lock];
 
 _threadCount = -2;
 //如果有n个要等待的thread,这里置成 -n
 [_myCondition unlock];
 //
 NSLog(@"");
 NSLog(@"------------------------------------------------------------------------------");
 [NSThread detachNewThreadSelector:@selector(threadOne) toTarget:self withObject:nil];
 [NSThread detachNewThreadSelector:@selector(threadTwo) toTarget:self withObject:nil];
 [NSThread detachNewThreadSelector:@selector(threadThree) toTarget:self withObject:nil];
 return;
}

- (void)threadOne{
 NSLog(@"@@@ In thread 111111 start.");
 [_myCondition lock];
 
 int n = rand()%5 + 1;
 NSLog(@"@@@ Thread 111111 Will sleep %d seconds ,now _threadCount is : %d",n,_threadCount);
 sleep(n);
 //[NSThread sleepForTimeInterval:n];
 _threadCount ++ ;
 NSLog(@"@@@ Thread 111111 has sleep %d seconds ,now _threadCount is : %d",n,_threadCount);
 [_myCondition signal];
 NSLog(@"@@@ Thread 1111111 has signaled ,now _threadCount is : %d",_threadCount);
 [_myCondition unlock];
 NSLog(@"@@@ In thread one complete.");
 [NSThread exit];
 return;
}

- (void)threadTwo{
 NSLog(@"### In thread 2222222 start.");
 [_myCondition lock];
 
 int n = rand()%5 + 1;
 NSLog(@"### Thread 2222222 Will sleep %d seconds ,now _threadCount is : %d",n,_threadCount);
 sleep(n);
 //   [NSThread sleepForTimeInterval:n];
 _threadCount ++ ;
 NSLog(@"### Thread 2222222 has sleep %d seconds ,now _threadCount is : %d",n,_threadCount);
 [_myCondition signal];
 NSLog(@"### Thread 2222222 has signaled ,now _threadCount is : %d",_threadCount);
 [_myCondition unlock];
 //_threadCount ++ ;
 NSLog(@"### In thread 2222222 complete.");
 [NSThread exit];
 return;
}

- (void)threadThree{
 NSLog(@"<<< In thread 333333 start.");
 [_myCondition lock];
 while (_threadCount < 0) {
  [_myCondition wait];
 }
 NSLog(@"<<< In thread 333333 ,_threadCount now is %d ,will start work.",_threadCount);
 [_myCondition unlock];
 NSLog(@"<<< In thread 333333 complete.");
 [NSThread exit];
 return;
}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
 // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
 
 // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
 // Release any retained subviews of the main view.
 // e.g. self.myOutlet = nil;
}


- (void)dealloc {
 [_myCondition release];
    [super dealloc];
}

@end

===================================================

【ios】iOS多线程编程:线程同步总结  

2012-07-04 11:34:03|  分类: ObjectiveC|字号 订阅

 
 
1:原子操作 - OSAtomic系列函数

iOS平台下的原子操作函数都以OSAtomic开头,使用时需要包含头文件<libkern/OSBase.h>。 不同线程如果通过原子操作函数对同一变量进行操作,可以保证一个线程的操作不会影响到其他线程内对此变量的操作,因为这些操作都是原子式的。因为原子操作 只能对内置类型进行操作,所以原子操作能够同步的线程只能位于同一个进程的地址空间内。

2:锁 - NSLock系列对象

iOS 平台下的锁对象为NSLock对象,进入锁通过调用lock函数,解锁调用unlock函数(因为iOS中大部分的线程同步类都继承自NSLocking 协议,所以其加锁/解锁的操作基本都为lock/unlock函数),同一个NSLock对象成功调用lock函数后,在其显式unlock之前任何线程 都不能再对此NSLock对象加锁,以达到互斥访问的目的。除了lock函数,对NSLock加锁的函数还包括tryLock以及 lockBeforeDate函数,lock函数在成功加锁之间会一直阻塞,而tryLock会尝试加锁,如果不成功,不会阻塞,而是直接返回 NO,lockBeforeDate则是阻塞到传入的NSDate日期为止。

除 了NSLock,iOS还提供了NSRecursive、NSConditionLock类型的锁类型。NSRecursive与NSLock最大的区别 就是NSRecursive是可重入的,也就是说一个线程可以对一个NSRecursive对象多次调用lock,只要解锁时调用相同次数的unlock 函数便可。NSConditionLock是一种带有条件的锁对象,除了基本的lock与unlock函数,还提供了lockWithCondition 以及unlockWithCondition,这两个函数接收整型类型的数据作为参数,只有当一个unlockWithCondition对象被调用时, 对应的lockWithCondition才会正常返回。这种机制在需几多个线程顺序化的完成某个任务时比较有用,例程如下:

  1. //线程A  
  2. id condLock = [[NSConditionLock alloc] initWithCondition:NO_DATA];  
  3.    
  4. while(true)  
  5. {  
  6.     [condLock lock];  
  7.     /* Add data to the queue. */  
  8.     [condLock unlockWithCondition:HAS_DATA];  
  9. }  
  1. //线程B  
  2. while (true)  
  3. {  
  4.     [condLock lockWhenCondition:HAS_DATA];  
  5.     /* Remove data from the queue. */  
  6.     [condLock unlockWithCondition:(isEmpty ? NO_DATA : HAS_DATA)];  
  7.    
  8.     // Process the data locally.  
  9. }  

除了显示的生成NSLock系列对象,还可以通过将代码放到@synchronized内来达到同步的目的,一段放入其内的代码,不同的线程是不能重入的例如:

  1. - (void)myMethod:(id)anObj  
  2. {  
  3.     @synchronized(anObj)  
  4.     {  
  5.         //此处代码在同一时刻只能有一个线程执行.  
  6.     }  
  7. }  

NSLock系列对象都是可以具名的,也就是说,这些对象可以用于不同进程内部的线程的同步。

3:事件 - NSCondtion

NSConditon 类型提供了wait与signal函数,分别代表了等待事件的操作以及触发事件的操作。除了wait函数,NSCondition还提供了 waitUntilDate函数,其功能与NSLock中的lockBeforeDate大致相同,简要来说就是提供了一个带超时的wait函数。

虽然NSCondition与Windows环境下Event类型所完成的功能大致类似,但对一个熟悉Event类型的开发人员来说,NSConditon的行为会有点奇怪:

第一点: 因为遵循NSLocking协议,所以NSCondition在触发与等待过程的前后要分别调用lock与unlock函数,前面提到过,当一个遵循 NSLocking协议的对象调用lock后,其他的对此对象的lock调用都会阻塞。那么,如果两个线程A和B,A要触发事件,B接收事件,B线程在调 用lock后,通过调用wait函数进入等待事件触发的状态,那么,A线程岂不是再也没有机会对这个事件进行触发了(因为此对象已经被B线程lock)? 秘密就在于wait函数的调用,其实,在wait函数内部悄悄的调用了unlock函数,也就是说在调用wati函数后,这个NSCondition对象 就处于了无锁的状态,这样A线程就可以对此对象加锁并触发该NSCondition对象。当一个事件被其他线程触发时,在wait函数内部得到此事件被触 发的通知,然后对此事件重新调用lock函数,然后函数返回,而在函数外部,看起来好像接收事件的线程从来没有放开NSCondition对象的所有 权,B线程直接由阻塞状态进入了触发状态。

第二点: 当有多个线程进入阻塞状态,等待同一个AutoReset的Event对象被触发时,在Windows环境下唤醒哪一个线程是没有固定的顺序的,也就是说 操作系统对唤醒哪一个线程不会提供任何的保证。而在iOS平台上,经过笔者测试,其被触发的顺序与,并且只与调用wait函数的顺序相关,与其他(比如线 程优先级)条件没有关系。这一点在开发时需要进行额外的考虑。

第三点:wait函数并不是完全可信的。这一点比较让人蛋疼,也就是说wait返回后,并不代表对应的事件一定被触发了,因此,为了保证线程之间的同步关系,使用NSCondtion时往往需要加入一个额外的变量来对非正常的wait返回进行规避。具体示例代码如下:

  1. //等待事件触发的线程  
  2. [cocoaCondition lock];  
  3. while (timeToDoWork <= 0)  
  4.     [cocoaCondition wait];  
  5.    
  6. timeToDoWork--;  
  7.    
  8. // Do real work here.  
  9.    
  10. [cocoaCondition unlock];  
  11.   
  12. //出发事件的线程  
  13. [cocoaCondition lock];  
  14. timeToDoWork++;  
  15. [cocoaCondition signal];  
  16. [cocoaCondition unlock];  


个timeToDoWork就是那个额外需要的变量,在NSCondition的使用中,这个变量是必不可少的。

NSConditon对象也是具名的,也就是说,其可于不同进程内部的线程同步。

 

 

相较于Windows平台下提供的丰富的线程同步机制,iOS下的线程同步机制稍显单薄,但也正是这种简洁简化了其使用。

 
 
 
 
 
原文地址:https://www.cnblogs.com/DamonTang/p/2721598.html