疑問?
在iOS中,經常需要用到線程的概念,尤其是類似UI繪制等操作,蘋果也明確規定必須在主線程進行繪制,那么,我們如何來判斷當前所在的線程呢?不同的判斷方法又有何區別呢?
1. pthread
pthread是一套通用使用使用C語言編寫的多線程的API,可以在Unix / Linux / Windows 等系統跨平臺使用。
常用API :
- pthread_create( )
/** Explain: 創建一個線程 */
- pthread_exit ( )
/** Explain: 退出當前線程 */
- pthread_main_np ( ) :
/** Explain: 獲取主線程 */
應用 :
if (pthread_main_np()) { // do something in main thread } else { // do something in other thread }
總結:
- 能夠判斷當前是否在主線程,不能判斷當前是否在主隊列。
- 需要引入頭文件 #import <pthread.h>
2. NSThread
NSThread是一套蘋果公司開發的使用Objective-C編寫的多線程API,其功能基本類同于pthread。
?由于GCD本身沒有提供判斷當前線程是否是主線程的API,因此我們常常使用NSThread中的API代替,這種是我們最常使用的方式。
常用API :
@property (readonly) BOOL isMainThread
/** Explain: 是否是主線程 */
- (void)cancel
/** Explain: 取消線程 */
- (void)start
/** Explain: 開始線程 */
應用:
if ([NSThread isMainThread]) { // do something in main thread } else { // do something in other thread }
這個方法在大部分時間內是有效的,在判斷當前是否是主線程的需求下與pthread_main_np()基本類同。
?同樣在部分情形中會有問題。
總結 :
- 該API只會檢查當前的線程是否是主線程,不會檢查是不是同時在主隊列。
- 每個app只有一個主線程,但是主線程上運行可能有很多不同的隊列在運行。
- 如果一個API調用任務在非主隊列上的主線程中被執行,會導致某些依賴主隊列運行的庫出現問題(如VectorKit)。
- 不需要引入頭文件。
參考一:GCD's Main Queue vs. Main Thread
參考二:[NSThread isMainThread] is probably not what you want
參考三:dispatch_queue_set_specific & dispatch_get_specific
3. dispatch_queue_set_specific() & dispatch_get_specific();
鑒于上面的方法都能夠判斷當前是否是在主線程,但是都不能判斷當前是否在主隊列的問題,我們很同意想到,只需要新增對當前隊列的判斷不是可以呢?
** 常用API :**
- ~~dispatch_get_current_queue() ~~【iOS6.0之后已被棄用】
/** Explain: 獲得當前隊列 */
- dispatch_queue_set_specific(dispatch_queue_t queue, const void *key,
void *_Nullable context, dispatch_function_t _Nullable destructor);
/** Explain: Sets the key/value data for the specified dispatch queue. // 通過設置key/value數據與指定的queue進行關聯。 【參數】: queue:需要關聯的queue,不允許傳入NULL。 key:唯一的關鍵字。 context:要關聯的內容,可以為NULL。 destructor:釋放context的函數,當新的context被設置時,destructor會被調用 */
- dispatch_get_specific(const void *key)
/** Explain: Returns the value for the key associated with the current dispatch queue. // 根據唯一的key取出當前queue的context,如果當前queue沒有key對應的context,則去queue的target queue取,取不著返回NULL,如果對全局隊列取,也會返回NULL。 【參數】 key:唯一的關鍵字。 */
** 應用 :**
static void *mainQueueKey = "mainQueueKey"; dispatch_queue_set_specific(dispatch_get_main_queue(), mainQueueKey, &mainQueueKey, NULL); if (dispatch_get_specific(mainQueueKey)) { // do something in main queue } else { // do something in other queue }
** 總結 :**
- 可以實現主隊列主線程的判斷。
- 代碼復雜,判斷是否是主隊列必須提前設定與主隊列的關聯關系。
- 擴展性強,可以用來區分自定義隊列。
4. dispatch_queue_get_label()
該方法我是在閱讀AFNetworking源碼時看到的,第一次見時挺奇怪作者為什么要這么寫,看起來好麻煩好復雜,今天,我們就一起瞧瞧這段代碼的優點吧。
常用API :
- dispatch_queue_get_label(dispatch_queue_t queue)
/** Explain: Returns the label specified for the queue when the queue was created. The label of the queue, or NULL if the queue was not provided a label during initialization. */
- strcmp(const char *s1, const char *s2)
/** Explain: strcmp() 用來比較字符串(區分大小寫)。 其原型為: int strcmp(const char *s1, const char *s2); 【返回值】: 若str1==str2,則返回零; 若str1<str2,則返回負數; 若str1>str2,則返回正數。 【參數】s1, s2 為需要比較的兩個字符串。 【注意】:strcmp() 以二進制的方式進行比較,不會考慮多字節或寬字節字符;如果考慮到本地化的需求,請使用 [strcoll()](http://c.biancheng.net/cpp/html/163.html) 函數。 */
- DISPATCH_CURRENT_QUEUE_LABEL
/** Explain: Pass this constant to the [dispatch_queue_get_label] function to retrieve the label of the current queue. */
應用 :
if (strcmp(dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL), dispatch_queue_get_label(dispatch_get_main_queue())) == 0) { // do something in main thread } else { // do something in other thread }
** 總結 :**
- 該方法不僅能夠判斷當前是否是主線程。
- 還可用來很好的判斷當前是否是在主隊列。