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才會正常返回。這種機制在需幾多個線程順序化的完成某個任務時比較有用,例程如下:

 

[plain]  view plain copy
  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. }  
[plain]  view plain copy
  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內來達到同步的目的,一段放入其內的代碼,不同的線程是不能重入的例如:

 

[plain]  view plain copy
  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返回進行規避。具體示例代碼如下:

 

[plain]  view plain copy
  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下的線程同步機制稍顯單薄,但也正是這種簡潔簡化了其使用。

 
 
 
 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM