ios GCD簡單介紹

來源:互聯網
上載者:User

標籤:

本從實踐出發簡單說明:

首先,gcd是Grand Central Dispatch的縮寫,意為多線程最佳化技術,是蘋果為多核處理最佳化的技術。使用簡單、清晰。

多線程就分同步、非同步方法呼叫如下:

//非同步線程開啟: dispatch_async(dispatch_queue_t queue, ^(void)block)//同步線程開啟: dispatch_sync(dispatch_queue_t queue, ^(void)block) //dispatch_queue_t:隊列 block:執行內容

隊列說明:

建立方法如下:

dispatch_queue_create(const char *label, dispatch_queue_attr_t attr)

參數說明:label為隊列名稱 attr為隊列形式   串列:DISPATCH_QUEUE_SERIAL  並行:DISPATCH_QUEUE_CONCURRENT 

特殊隊列說明:

系統預設就有一個串列隊列main_queue和並行隊列global_queue 使用方法如下:

dispatch_get_main_queue()//主隊列dispatch_get_global_queue(long identifier, unsigned long flags);/*identifler:優先順序 *  - DISPATCH_QUEUE_PRIORITY_HIGH:    2     *  - DISPATCH_QUEUE_PRIORITY_DEFAULT:      0 *  - DISPATCH_QUEUE_PRIORITY_LOW:      -2 *  - DISPATCH_QUEUE_PRIORITY_BACKGROUND: flags:待開發屬性 請輸入0 */

最後,特殊線程:

 //前面的線程都結束後執行 此線程結束後才執行它後面的線程 dispatch_barrier_async(dispatch_queue_t queue, ^(void)block) dispatch_barrier_sync(dispatch_queue_t queue, ^(void)block)//重複執行線程 iterations 次數 dispatch_apply(size_t iterations, dispatch_queue_t queue, ^(size_t)block)//只執行一次 static dispatch_once_t onceToken;      dispatch_once(&onceToken, ^{          // code to be executed once      }); //順延強制 NSEC_PER_SEC 秒dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);      dispatch_after(popTime, dispatch_get_main_queue(), ^(void){          // code to be executed on the main queue after delay      });  //設定一個dispatch queue的優先順序void dispatch_set_target_queue( dispatch_object_t object, dispatch_queue_t queue)

 

ios GCD簡單介紹

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.