什么是多線程
多線程是一個比較輕量級的方法來實現單個應用程序內多個代碼執行路徑。從技術角度來看,一個線程就是一個需要管理執行代碼的內核級和應用級數據結構組合。內核級結構協助調度線程事件,並搶占式調度一個線程到可用的內核之上。應用級結構包括用於存儲函數調用的調用堆棧和應用程序需要管理和操作線程屬性和狀態的結構。
多線程的替代方法
你自己創建多線程代碼的一個問題就是它會給你的代碼帶來不確定性。多線程是一個相對較低的水平和復雜的方式來支持你的應用程序並發。如果你不完全理解你的設計選擇的影響,你可能很容易遇到同步或定時問題,其范圍可以從細微的行為變化到嚴重到讓你的應用程序崩潰並破壞用戶數據。
你需要考慮的另一個因素是你是否真的需要多線程或並發。多線程解決了如何在同一個進程內並發的執行多路代碼路徑的問題。然而在很多情況下你是無法保證你所在做的工作是並發的。多線程引入帶來大量的開銷,包括內存消耗和CPU占用。你會發現這些開銷對於你的工作而言實在太大,或者有其他方法會更容易實現。
1、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.
2、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
3、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 Topics.
4、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.
5、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.”
6、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.
線程支持
在應用層上,其他平台一樣所有線程的行為本質上是相同的。線程啟動之后,線程就進入三個狀態中的任何一個:運行(running)、就緒(ready)、阻塞(blocked)。如果一個線程當前沒有運行,那么它不是處於阻塞,就是等待外部輸入,或者已經准備就緒等待分配CPU。線程持續在這三個狀態之間切換,直到它最終退出或者進入中斷狀態。
1、Cocoa threads
Cocoa implements threads using the NSThread class. Cocoa also provides methods onNSObject for spawning new threads and executing code on already-running threads. For more information, see “Using NSThread” and “Using NSObject to Spawn a Thread.”
2、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”
3、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 Mac 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.
同步工具
線程編程的危害之一是在多個線程之間的資源爭奪。如果多個線程在同一個時間試圖使用或者修改同一個資源,就會出現問題。緩解該問題的方法之一是消除共享資源,並確保每個線程都有在它操作的資源上面的獨特設置。因為保持完全獨立的資源是不可行的,所以你可能必須使用鎖,條件,原子操作和其他技術來同步資源的訪問。
鎖提供了一次只有一個線程可以執行代碼的有效保護形式。最普遍的一種鎖是互斥排他鎖,也就是我們通常所說的“mutex”。當一個線程試圖獲取一個當前已經被其他線程占據的互斥鎖的時候,它就會被阻塞直到其他線程釋放該互斥鎖。系統的幾個框架提供了對互斥鎖的支持,雖然它們都是基於相同的底層技術。此外Cocoa提供了幾個互斥鎖的變種來支持不同的行為類型,比如遞歸。
除了鎖,系統還提供了條件,確保在你的應用程序任務執行的適當順序。一個條件作為一個看門人,阻塞給定的線程,直到它代表的條件變為真。當發生這種情況的時候,條件釋放該線程並允許它繼續執行。POSIX級別和基礎框架都直接提供了條件的支持。(如果你使用操作對象,你可以配置你的操作對象之間的依賴關系的順序確定任務的執行順序,這和條件提供的行為非常相似)。
盡管鎖和條件在並發設計中使用非常普遍,原子操作也是另外一種保護和同步訪問數據的方法。原子操作在以下情況的時候提供了替代鎖的輕量級的方法,其中你可以執行標量數據類型的數學或邏輯運算。原子操作使用特殊的硬件設施來保證變量的改變在其他線程可以訪問之前完成。
線程間通信
線程間通信有很多種方法,每種都有它的優點和缺點。
1、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 “Cocoa Perform Selector Sources.”
2、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.
3、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 “Using Conditions.”
4、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.”
5、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.”
6、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.
7、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, seeDistributed Objects Programming Topics.
設計技巧
1、避免顯式創建線程
手動編寫線程創建代碼是乏味的,而且容易出現錯誤,你應該盡可能避免這樣做。Mac OS X和iOS通過其他API接口提供了隱式的並發支持。你可以考慮使用異步API,GCD方式,或操作對象來實現並發,而不是自己創建一個線程。這些技術背后為你做了線程相關的工作,並保證是無誤的。此外,比如GCD和操作對象技術被設計用來管理線程,比通過自己的代碼根據當前的負載調整活動線程的數量更高效。 關於更多GCD和操作對象的信息,你可以查閱“並發編程指南(Concurrency Programming Guid)”。
2、保持你的線程合理的忙
如果你准備人工創建和管理線程,記得多線程消耗系統寶貴的資源。你應該盡最大努力確保任何你分配到線程的任務是運行相當長時間和富有成效的。同時你不應該害怕中斷那些消耗最大空閑時間的線程。
3、 避免共享數據結構
避免造成線程相關資源沖突的最簡單最容易的辦法是給你應用程序的每個線程一份它需求的數據的副本。最小化線程之間的通信和資源爭奪時並行代碼的效果最好。
4、多線程和你的用戶界面
如果你的應用程序具有一個圖形用戶界面,建議你在主線程里面接收和界面相關的事件和初始化更新你的界面。這種方法有助於避免與處理用戶事件和窗口繪圖相關的同步問題。一些框架,比如Cocoa,通常需要這樣操作,但是它的事件處理可以不這樣做,在主線程上保持這種行為的優勢在於簡化了管理你應用程序用戶界面的邏輯。
有幾個顯著的例外,它有利於在其他線程執行圖形操作。比如,QuickTime API包含了一系列可以在輔助線程執行的操作,包括打開視頻文件,渲染視頻文件,壓縮視頻文件,和導入導出圖像。類似的,在Carbon和Cocoa里面,你可以使用輔助線程來創建和處理圖片和其他圖片相關的計算。使用輔助線程來執行這些操作可以極大提高性能。如果你不確定一個操作是否和圖像處理相關,那么你應該在主線程執行這些操作。
關於QuickTime線程安全的信息,查閱Technical Note TN2125:“QuickTime的線程安全編程”。關於Cocoa線程安全的更多信息,查閱“線程安全總結”。關於Cocoa繪畫信息,查閱Cocoa繪畫指南(Cocoa Drawing Guide)。
5、了解線程退出時的行為
進程一直運行直到所有非獨立線程都已經退出為止。默認情況下,只有應用程序的主線程是以非獨立的方式創建的,但是你也可以使用同樣的方法來創建其他線程。當用戶退出程序的時候,通常考慮適當的立即中斷所有獨立線程,因為通常獨立線程所做的工作都是是可選的。如果你的應用程序使用后台線程來保存數據到硬盤或者做其他周期行的工作,那么你可能想把這些線程創建為非獨立的來保證程序退出的時候不丟失數據。
以非獨立的方式創建線程(又稱作為可連接的)你需要做一些額外的工作。因為大部分上層線程封裝技術默認情況下並沒有提供創建可連接的線程,你必須使用POSIX API來創建你想要的線程。此外,你必須在你的主線程添加代碼,來當它們最終退出的時候連接非獨立的線程。更多有關創建可連接的線程信息,請查閱“設置線程的脫離狀態”部分。
如果你正在編程Cocoa的程序,你也可以通過使用applicationShouldTerminate:的委托方法來延遲程序的中斷直到一段時間后或者完成取消。當延遲中斷的時候,你的程序需要等待直到任何周期線程已經完成它們的任務且調用了replyToApplicationShouldTerminate:方法。關於更多這些方法的信息,請查閱NSApplication Class Reference。
6、處理異常
當拋出一個異常時,異常的處理機制依賴於當前調用堆棧執行任何必要的清理。因為每個線程都有它自己的調用堆棧,所以每個線程都負責捕獲它自己的異常。如果在輔助線程里面捕獲一個拋出的異常失敗,那么你的主線程也同樣捕獲該異常失敗:它所屬的進程就會中斷。你無法捕獲同一個進程里面其他線程拋出的異常。
如果你需要通知另一個線程(比如主線程)當前線程中的一個特殊情況,你應該捕捉異常,並簡單地將消息發送到其他線程告知發生了什么事。根據你的模型和你正在嘗試做的事情,引發異常的線程可以繼續執行(如果可能的話),等待指示,或者干脆退出。
7、干凈地中斷你的線程
線程自然退出的最好方式是讓它達到其主入口結束點。雖然有不少函數可以用來立即中斷線程,但是這些函數應僅用於作為最后的手段。在線程達到它自然結束點之前中斷一個線程阻礙該線程清理完成它自己。如果線程已經分配了內存,打開了文件,或者獲取了其他類型資源,你的代碼可能沒辦法回收這些資源,結果造成內存泄漏或者其他潛在的問題。
關於更多正確退出線程的信息,請查閱“中斷線程”部分。
8、 線程安全的庫
雖然應用程序開發人員控制應用程序是否執行多個線程,類庫的開發者則無法這樣控制。當開發類庫時,你必須假設調用應用程序是多線程,或者多線程之間可以隨時切換。因此你應該總是在你的臨界區使用鎖功能。
對類庫開發者而言,只當應用程序是多線程的時候才創建鎖是不明智的。如果你需要鎖定你代碼中的某些部分,早期應該創建鎖對象給你的類庫使用,更好是顯式調用初始化類庫。雖然你也可以使用靜態庫的初始化函數來創建這些鎖,但是僅當沒有其他方式的才應該這樣做。執行初始化函數需要延長加載你類庫的時間,且可能對你程序性能造成不利影響。