--参考:(官网) https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/Multithreading/Introduction/Introduction.html + (中文翻译) http://www.cocoachina.com/bbs/read.php?tid=87592
多线程是一个比较轻量级的方法在单个应用程序内实现同步完成多项任务。在系统级别内,程序并排执行,系统分配到每个程序的执行时间是基于该程序的所需时间 和其他程序的所需时间来决定的。然而在每个应程序的内部,存在一个或多个执行线 程,它同时或在一个几乎同时发生的方式里执行不同的任务。系统本身管理这些执行 的线程,调度它们在可用的内核上运行,并在需要让其他线程执行的时候抢先打断它们。
从技术角度来看,一个线程就是一个需要管理执行代码的内核级和应用级数据结 构组合。内核级结构协助调度线程事件,并抢占式调度一个线程到可用的内核之上。 应用级结构包括用于存储函数调用的调用堆栈和应用程序需要管理和操作线程属性 和状态的结构。
在非并发的应用程序,只有一个执行线程。该线程开始和结束于你应用程序的 main 循环,一个个方法和函数的分支构成了你整个应用程序的所有行为。与此相反, 支持并发的应用程序开始可以在需要额外的执行路径时候创建一个或多个线程。每个 新的执行路径有它自己独立于应用程序 main 循环的定制开始循环。在应用程序中存 在多个线程提供了两个非常重要的的潜在优势: 多个线程可以提高应用程序的感知响应。 多个线程可以提高应用程序在多核系统上的实时性能。
如果你的应用程序只有单独的线程,那么该独立程序需要完成所有的事情。它必须对事件作出响应,更新您的应用程序的窗口,并执行所有实现你应用程序行为需要 的计算。拥有单独线程的主要问题是在同一时间里面它只能执行一个任务。那么当你 的应用程序需要很长时间才能完成的时候会发生什么呢?当你的代码忙于计算你所需要的值的时候,你的程序就会停止响应用户事件和更新它的窗口。如果这样的情况 持续足够长的时间,用户就会误认为你的程序被挂起了,并试图强制退出。如果你把 你的计算任务转移到一个独立的线程里面,那么你的应用程序主线程就可以自由并及 时响应用户的交互。
当然多线程并不是解决程序性能问题的灵丹妙药。多线程带来好处同时也伴随着 潜在问题。应用程序内拥有多个可执行路径,会给你的代码增加更多的复杂性。每个线程需要和其他线程协调其行为,以防止它破坏应用程序的状态信息。因为应用程序内的多个线程共享内存空间,它们访问相同的数据结构。如果两个线程试图同时处理相同的数据结构,一个线程有可能覆盖另外线程的改动导致破坏该数据结构。即使有适当的保护,你仍然要注意由于编译器的优化导致给你代码产生很微妙的(和不那么微妙)的Bug。
--线程(thread)用于指代独立执行的代码段。
--进程(process)用于指代一个正在运行的可执行程序,它可以包含多个线程。
--任务(task)用于指代抽象的概念,表示需要执行工作
你自己创建多线程代码的一个问题就是它会给你的代码带来不确定性。多线程是一个相对底层的水平和复杂的方式来支持你的应用程序并发。如果你不完全理解你的 设计选择的影响,你可能很容易遇到同步或定时问题,其范围可以从细微的行为变化到严重到让你的应用程序崩溃并破坏用户数据。
你需要考虑的另一个因素是你是否真的需要多线程或并发。多线程解决了如何在同一个进程内并发的执行多路代码路径的问题。然而在很多情况下你是无法保证你所在做的工作是并发的。多线程引入带来大量的开销,包括内存消耗和 CPU 占用。你会发现这些开销对于你的工作而言实在太大,或者有其他方法会更容易实现。下面列举了多线程的替代方法:
技术 | 描述 |
Operation objects | Introduced in Mac OS X v10.5, an operation object is a wrapper for a task that would normally be executed on a secondary thread. This wrapper hides the thread management aspects of performing the task, leaving you free to focus on the task itself. You typically use these objects in conjunction with an operation queue object, which actually manages the execution of the operation objects on one more threads. For more information on how to use operation objects, see Concurrency Programming Guide |
Grand Central Dispatch (GCD)  |
Introduced in Mac OS x v10.6, Grand Central Dispatch is another alternative to threads that lets you focus on the tasks you need to perform rather than on thread management. With GCD, you define the task you want to perform and add it to a work queue, which handles the scheduling of your task on an appropriate thread. Work queues take into account the number of available cores and the current load to execute your tasks more efficiently than you could do yourself using threads. For information on how to use GCD and work queues, see Concurrency Programming Guide |
Idle-time notifications | For tasks that are relatively short and very low priority, idle time notifications let you perform the task at a time when your application is not as busy. Cocoa provides support for idle-time notifications using the NSNotificationQueue object. To request an idle-time notification, post a notification to the default NSNotificationQueue object using the NSPostWhenIdle option. The queue delays the delivery of your notification object until the run loop becomes idle. For more information, see Notification Programming Top |
Asynchronous functions | The system interfaces include many asynchronous functions that provide automatic concurrency for you. These APIs may use system daemons and processes or create custom threads to perform their task and return the results to you. (The actual implementation is irrelevant because it is separated from your code.) As you design your application, look for functions that offer asynchronous behavior and consider using them instead of using the equivalent synchronous function on a custom thread. |
Timers | You can use timers on your application’s main thread to perform periodic tasks that are too trivial to require a thread, but which still require servicing at regular intervals. For information on timers, see “Timer Sources. |
Separate processes |
Although more heavyweight than threads, creating a separate process might be useful in cases where the task is only tangentially related to your application. You might use a process if a task requires a significant amount of memory or must be executed using root privileges. For example, you might use a 64-bit server process to compute a large data set while your 32-bit application displays the results to the user. |
如果你已经有代码使用了多线程,Mac OS X 和 iOS 提供几种技术来在你的应用程 序里面创建多线程。此外,两个系统都提供了管理和同步你需要在这些线程里面处理 的工作。以下几个部分描述了一些你在 Mac OS X 和 iOS 上面使用多线程的时候需要注意的关键技术:
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”小节和“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. |
Direct messaging | Cocoa applications support the ability to perform selectors directly on other threads. This capability means that one thread can essentially execute a method on any other thread. Because they are executed in the context of the target thread, messages sent this way are automatically serialized on that thread. For information about input sources, see “Run Loops”章节 |
Global variables, shared memory, and objects | Another simple way to communicate information between two threads is to use a global variable, shared object, or shared block of memory. Although shared variables are fast and simple, they are also more fragile than direct messaging. Shared variables must be carefully protected with locks or other synchronization mechanisms to ensure the correctness of your code. Failure to do so could lead to race conditions, corrupted data, or crashes. |
Conditions | Conditions are a synchronization tool that you can use to control when a thread executes a particular portion of code. You can think of conditions as gate keepers, letting a thread run only when the stated condition is met. For information on how to use conditions, see “同步工具”小节 |
Run loop sources | A custom run loop source is one that you set up to receive application-specific messages on a thread. Because they are event driven, run loop sources put your thread to sleep automatically when there is nothing to do, which improves your thread’s efficiency. For information about run loops and run loop sources, see “Run Loops.”小节 |
Ports and sockets | Port-based communication is a more elaborate way to communication between two threads, but it is also a very reliable technique. More importantly, ports and sockets can be used to communicate with external entities, such as other processes and services. For efficiency, ports are implemented using run loop sources, so your thread sleeps when there is no data waiting on the port. For information about run loops and about port-based input sources,see “Run Loops.”小节 |
Message queues | The legacy Multiprocessing Services defines a first-in, first-out (FIFO) queue abstraction for managing incoming and outgoing data. Although message queues are simple and convenient, they are not as efficient as some other communications techniques. For more information about how to use message queues, see Multiprocessing Services Programming Guide. |
Cocoa distributed objects | Distributed objects is a Cocoa technology that provides a high-level implementation of port-based communications. Although it is possible to use this technology for inter-thread communication, doing so is highly discouraged because of the amount of overhead it incurs. Distributed objects is much more suitable for communicating with other processes, where the overhead of going between processes is already high. For more information, see Distributed Objects Programming Topics. |
进程(process)一直运行直到所有non-detached线程都已经退出为止。默认情况下,只有应用程序的主线程是non-detached的,但是你也可以使用同样的方法来创建其他线程。 当用户退出程序的时候,通常考虑适当的立即中断所有detached线程,因为通常detached线程所做的工作都是是可选(optional)的。如果你的应用程序要使用后台线程来保存数据到硬盘或者做其他周期行的工作,那么你可能需要把这些线程创建为non-detached的,来保证程序退出的时候不丢失数据。
Kernel data structures | Approximately 1 KB | This memory is used to store the thread data structures and attributes, much of which is allocated as wired memory and therefore cannot be paged to disk. |
Stack space | 512 KB (secondary threads) 8 MB (OS X main thread) 1 MB (iOS main thread) | The minimum allowed stack size for secondary threads is 16 KB and the stack size must be a multiple of 4 KB. The space for this memory is set aside in your process space at thread creation time, but the actual pages associated with that memory are not created until they are needed. |
Creation time | Approximately 90 microseconds | This value reflects the time between the initial call to create the thread and the time at which the thread’s entry point routine began executing. The figures were determined by analyzing the mean and median values generated during thread creation on an Intel-based iMac with a 2 GHz Core Duo processor and 1 GB of RAM running OS X v10.5 |
[NSThread detachNewThreadSelector:@selector(myThreadMainMethod:) toTarget:self withObject:nil];在Mac OS X v10.5之前,你使用NSThread类来生成(spawn)多线程。虽然你可以获取一个NSThread对象并访问线程的属性,但你只能在线程运行之后在其内部做到这些。在Mac OS X v10.5支持 创建一个NSThread对象,而无需立即生成一个相应的新线程(这些在iOS里面同样可用)。新版支持使得在线程启动之前获取并设置线程的很多属性成为可能。 这也让用线程对象来引用正在运行的线程成为可能。
NSThread* myThread = [[NSThread alloc] initWithTarget:self selector:@selector(myThreadMainMethod:) object:nil]; [myThread start]; // Actually create the thread备注:使用initWithTarget:selector:object:方法的替代办法是子类化NSThread,并重写它的main方法。你可以使用你重写的该方法的版本来实现你线程的主体入口。更多信息,请参阅 NSThread Class Reference 里面subclassing notes。
#include <assert.h> #include <pthread.h> void* PosixThreadMainRoutine(void* data) { // Do some work here. return NULL; } void LaunchThread() { // Create the thread using POSIX routines. pthread_attr_t attr; pthread_t posixThreadID; int returnVal; returnVal = pthread_attr_init(&attr); assert(!returnVal); returnVal = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); assert(!returnVal); int threadError = pthread_create(&posixThreadID, &attr, &PosixThreadMainRoutine, NULL); returnVal = pthread_attr_destroy(&attr); assert(!returnVal); if (threadError != 0) { // Report an error. } }如果你把上面列表的代码添加到你任何一个源文件,并且调用LaunchThread函数,它将会在你的应用程序里面创建一个新的脱离线程。当然,新创建的线程使用该代码没有做任何有用的事情。线程将会加载并立即退出。为了让它更有兴趣,你需要添加代码到PosixThreadMainRoutine函数里面来做一些实际的工作。为了保证线程知道该干什么,你可以在创建的时候给线程传递一个数据的指针。把该指针作为pthread_create的最后一个参数。
// NSThread.h @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);
<span style="font-family: Arial, Helvetica, sans-serif;">[myObj performSelectorInBackground:@selector(doSomething) withObject:nil];</span>调用该方法的效果和你在当前对象里面使用NSThread的detachNewThreadSelector:toTarget:withObject:传递selectore,object作为参数的方法一样。新的线程将会被立即生成并运行,它使用默认的设置。在selectore内部,你必须配置线程就像你在任何线程里面一样。比如,你可能需要设置一个自动释放池(如果你没有使用垃圾回收机制),配置线程的run loop如果你需要使用它。关于如果配置线程的信息,详见“ 配置线程属性”小节
尽管NSThread类是Cocoa应用程序里面创建多线程的主要接口,如果方便的话你可以任意使用POSIX线程带替代。例如,如果你的代码里面已经使用了POSIX线程,而你又不想改写它的话,这时你可能需要使用POSIX多线程。如果你真打算在Cocoa程序里面使用POSIX线程,你应该了解如果在Cocoa和线程间交互,并遵循以下部分的一些指南:
1.保护Cocoa框架:
对于多线程的应用程序,Cocoa框架使用锁和其他内部同步(internal synchronization)方式来保证代码的正确执行。为了防止这些锁造成在单线程里面性能的损失,Cocoa直到应用程序使用NSThread类生成它的第一个新的线程的时候才创建这些锁。如果你仅且使用POSIX例程来生成新的线程,Cocoa不会收到关于你的应用程序当前变为多线程的通知。当这些刚好发生的时候,涉及Cocoa框架的操作可能会破坏甚至让你的应用程序崩溃。
2.混合POSIX和Cocoa的锁:
在同一个应用程序里面混合使用POSIX和Cocoa的锁很安全。Cocoa锁和条件对象(condition objects)基本上封装了POSIX的互斥体和条件。对于给定一个锁,你必须总是使用同样的接口来创建和操纵该锁。换言之,你不能使用Cocoa的NSLock对象来操纵一个你使用pthread_mutex_init函数生成的互斥体(mutex),反之亦然。
Cocoa | In iOS and OS X v10.5 and later, allocate and initialize an NSThread object (do not use the detachNewThreadSelector: toTarget:withObject: method). Before calling the start method of the thread object, use the setStackSize: method to specify the new stack size. |
POSIX | Create a new pthread_attr_t structure and use the pthread_attr_ -setstacksize function to change the default stack size. Pass the attributes to the pthread_create function when creating your thread. |
Multiprocessing Services | Pass the appropriate stack size value to the MPCreateTask function when you create your thread. |
<pre name="code" class="objc">//NSThread.h- (double)threadPriority NS_AVAILABLE(10_6, 4_0); // 优先级- (void)setThreadPriority:(double)p NS_AVAILABLE(10_6, 4_0);
- (NSMutableDictionary *)threadDictionary; //本地存储- (NSUInteger)stackSize NS_AVAILABLE(10_5, 2_0); // 栈大小- (void)setStackSize:(NSUInteger)s NS_AVAILABLE(10_5, 2_0);
- (void)myThreadMainRoutine { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; // Top-level pool // Do thread work here. [pool release]; // Release the objects in the pool. }因为顶层的自动释放池不会被释放直到线程退出。长时运行的线程应该 新建额外的自动释放池来更频繁的释放它的对象。比如,一个使用run loop的线程可能在每次运行完一次循环的时候创建并释放该自动释放池。更频繁的释放对象可以防止你的应用程序内存占用( memory footprint)太大造成性能问题。对于任何与性能相关的行为,你应该测量你代码的实际表现,并适当地调整使用自动释放池。关于更多内存管理的信息和自动释放池,参阅“ Advanced Memory Management Programming Guide”
- (void)threadMainRoutine { BOOL moreWorkToDo = YES; BOOL exitNow = NO; NSRunLoop* runLoop = [NSRunLoop currentRunLoop]; // Add the exitNow BOOL to the thread dictionary. NSMutableDictionary* threadDict = [[NSThread currentThread] threadDictionary]; [threadDict setValue:[NSNumber numberWithBool:exitNow] forKey:@"ThreadShouldExitNow"]; // Install an input source. [self myInstallCustomInputSource]; while (moreWorkToDo && !exitNow) { // Do one chunk of a larger body of work here. // Change the value of the moreWorkToDo Boolean when done. // Run the run loop but timeout immediately if the input source isn't waiting to fire. [runLoop runUntilDate:[NSDate date]]; // Check to see if an input source handler changed the exitNow value. exitNow = [[threadDict valueForKey:@"ThreadShouldExitNow"] boolValue]; } }------------------------------------------------------------------我是分割线-------------------------------------------------------------------------