IOS對於執行緒佇列的一些理解
第一部分:執行緒與佇列
執行緒是程式碼執行的路徑,佇列則是用於儲存以及管理任務的,執行緒負責去佇列中取任務進行執行。
例如:在主執行緒呼叫如下程式碼
- dispatch_sync(queue, ^{
- task()
- });
- -(void)task
- {
- NSLog(@"1. 當前執行緒是: %@, 當前佇列是: %@ 。",[NSThread currentThread],dispatch_get_current_queue());
- }
- dispatch_async(queue, ^{
-
task()
- });
- void func(dispatch_queue_t queue, dispatch_block_t block)
- {
- if (dispatch_get_current_queue() == queue) {
- block();
- }else{
- dispatch_sync(queue, block);
-
}
- }
- - (void)deadLockFunc
- {
- dispatch_queue_t queueA = dispatch_queue_create("com.yiyaaixuexi.queueA", NULL);
- dispatch_queue_t queueB = dispatch_queue_create("com.yiyaaixuexi.queueB", NULL);
- dispatch_sync(queueA, ^{
- dispatch_sync(queueB, ^{
-
dispatch_block_t block = ^{
- //do something
- };
- func(queueA, block);
- });
- });
- }
那麼應該如何保證GCD方法可重入呢?
- dispatch_queue_set_specific 標記佇列
- if (dispatch_get_specific(queueKey1)) {
- //說明當前的佇列就是queue1
- }else{
- </span>//說明當前的佇列不是是queue1
- }
- #import <Foundation/Foundation.h>
- int main(int argc, constcharchar * argv[]) {
- @autoreleasepool {
- staticvoidvoid *queueKey1 = "queueKey1";
- dispatch_queue_t queue1 = dispatch_queue_create(queueKey1, DISPATCH_QUEUE_SERIAL);
- dispatch_queue_set_specific(queue1, queueKey1, &queueKey1, NULL);
- NSLog(@"1. 當前執行緒是: %@, 當前佇列是: %@ 。",[NSThread currentThread],dispatch_get_current_queue());
- if (dispatch_get_specific(queueKey1)) {
- //當前佇列是主佇列,不是queue1佇列,所以取不到queueKey1對應的值,故而不執行
- NSLog(@"2. 當前執行緒是: %@, 當前佇列是: %@ 。",[NSThread currentThread],dispatch_get_current_queue());
- [NSThread sleepForTimeInterval:1];
- }else{
- NSLog(@"3. 當前執行緒是: %@, 當前佇列是: %@ 。",[NSThread currentThread],dispatch_get_current_queue());
- [NSThread sleepForTimeInterval:1];
- }
- dispatch_sync(queue1, ^{
- NSLog(@"4. 當前執行緒是: %@, 當前佇列是: %@ 。",[NSThread currentThread],dispatch_get_current_queue());
- [NSThread sleepForTimeInterval:1];
- if (dispatch_get_specific(queueKey1)) {
- //當前佇列是queue1佇列,所以能取到queueKey1對應的值,故而執行
- NSLog(@"5. 當前執行緒是: %@, 當前佇列是: %@ 。",[NSThread currentThread],dispatch_get_current_queue());
- [NSThread sleepForTimeInterval:1];
- }else{
- NSLog(@"6. 當前執行緒是: %@, 當前佇列是: %@ 。",[NSThread currentThread],dispatch_get_current_queue());
- [NSThread sleepForTimeInterval:1];
- }
- });
- dispatch_async(queue1, ^{
- NSLog(@"7. t當前執行緒是: %@, 當前佇列是: %@ 。",[NSThread currentThread],dispatch_get_current_queue());
- [NSThread sleepForTimeInterval:1];
- });
- [NSThread sleepForTimeInterval:5];
- }
- return0;
- }
task函式這個任務將會提交到佇列queue上,主執行緒負責去執行這個任務。
dispatch_get_current_queue這個方法可以很方便的獲取到當前執行緒,可為什麼要丟棄這個函式呢?請看一下程式碼
為了防止類似的誤用,蘋果在iOS6廢棄了dispatch_get_current_queue()方法。強大的dispatch_get_current_queue()也只能當作一個除錯工具了。
dispatch_queue_set_specific就是向指定佇列裡面設定一個標識 如:
dispatch_queue_set_specific(queue1, queueKey1, &queueKey1,NULL);
就是向queue1對了中設定一個queueKey1標識。
輸出結果:
2016-02-19 14:31:23.390 gcd[96865:820267] 1.當前執行緒是: <NSThread: 0x1001053e0>{number = 1, name = main},當前佇列是: <OS_dispatch_queue: com.apple.main-thread[0x100059ac0]>。
2016-02-19 14:31:23.391 gcd[96865:820267] 3.當前執行緒是: <NSThread: 0x1001053e0>{number = 1, name = main},當前佇列是: <OS_dispatch_queue: com.apple.main-thread[0x100059ac0]>。
2016-02-19 14:31:24.396 gcd[96865:820267] 4.當前執行緒是: <NSThread: 0x1001053e0>{number = 1, name = main},當前佇列是: <OS_dispatch_queue: queueKey1[0x103000000]>。
2016-02-19 14:31:25.400 gcd[96865:820267] 5.當前執行緒是: <NSThread: 0x1001053e0>{number = 1, name = main},當前佇列是: <OS_dispatch_queue: queueKey1[0x103000000]>。
2016-02-19 14:31:26.402 gcd[96865:820367] 7. t當前執行緒是: <NSThread: 0x100105e10>{number = 2, name = (null)},當前佇列是: <OS_dispatch_queue: queueKey1[0x103000000]>。
Program ended with exit code: 0