iOS GCD全析(特別篇)

GCD頭文件里那些像dispatch_async_f后面帶 f 的函數(shù)是干嘛的?

我們先來看看源文件中有哪些后面帶 f 的函數(shù):

/*!
 * @function dispatch_async_f
 *
 * @abstract
 * Submits a function for asynchronous execution on a dispatch queue.
 *
 * @discussion
 * See dispatch_async() for details.
 *
 * @param queue
 * The target dispatch queue to which the function is submitted.
 * The system will hold a reference on the target queue until the function
 * has returned.
 * The result of passing NULL in this parameter is undefined.
 *
 * @param context
 * The application-defined context parameter to pass to the function.
 *
 * @param work
 * The application-defined function to invoke on the target queue. The first
 * parameter passed to this function is the context provided to
 * dispatch_async_f().
 * The result of passing NULL in this parameter is undefined.
 */
API_AVAILABLE(macos(10.6), ios(4.0))
DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NONNULL3 DISPATCH_NOTHROW
void
dispatch_async_f(dispatch_queue_t queue,
    void *_Nullable context,
    dispatch_function_t work);
/*!
 * @function dispatch_sync_f
 *
 * @abstract
 * Submits a function for synchronous execution on a dispatch queue.
 *
 * @discussion
 * See dispatch_sync() for details.
 *
 * @param queue
 * The target dispatch queue to which the function is submitted.
 * The result of passing NULL in this parameter is undefined.
 *
 * @param context
 * The application-defined context parameter to pass to the function.
 *
 * @param work
 * The application-defined function to invoke on the target queue. The first
 * parameter passed to this function is the context provided to
 * dispatch_sync_f().
 * The result of passing NULL in this parameter is undefined.
 */
API_AVAILABLE(macos(10.6), ios(4.0))
DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NONNULL3 DISPATCH_NOTHROW
void
dispatch_sync_f(dispatch_queue_t queue,
    void *_Nullable context,
    dispatch_function_t work);
/*!
 * @function dispatch_barrier_async_f
 *
 * @abstract
 * Submits a barrier function for asynchronous execution on a dispatch queue.
 *
 * @discussion
 * Submits a function to a dispatch queue like dispatch_async_f(), but marks
 * that function as a barrier (relevant only on DISPATCH_QUEUE_CONCURRENT
 * queues).
 *
 * See dispatch_async_f() for details.
 *
 * @param queue
 * The target dispatch queue to which the function is submitted.
 * The system will hold a reference on the target queue until the function
 * has returned.
 * The result of passing NULL in this parameter is undefined.
 *
 * @param context
 * The application-defined context parameter to pass to the function.
 *
 * @param work
 * The application-defined function to invoke on the target queue. The first
 * parameter passed to this function is the context provided to
 * dispatch_barrier_async_f().
 * The result of passing NULL in this parameter is undefined.
 */
API_AVAILABLE(macos(10.7), ios(4.3))
DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NONNULL3 DISPATCH_NOTHROW
void
dispatch_barrier_async_f(dispatch_queue_t queue,
    void *_Nullable context,
    dispatch_function_t work);
/*!
 * @function dispatch_barrier_sync_f
 *
 * @abstract
 * Submits a barrier function for synchronous execution on a dispatch queue.
 *
 * @discussion
 * Submits a function to a dispatch queue like dispatch_sync_f(), but marks that
 * fuction as a barrier (relevant only on DISPATCH_QUEUE_CONCURRENT queues).
 *
 * See dispatch_sync_f() for details.
 *
 * @param queue
 * The target dispatch queue to which the function is submitted.
 * The result of passing NULL in this parameter is undefined.
 *
 * @param context
 * The application-defined context parameter to pass to the function.
 *
 * @param work
 * The application-defined function to invoke on the target queue. The first
 * parameter passed to this function is the context provided to
 * dispatch_barrier_sync_f().
 * The result of passing NULL in this parameter is undefined.
 */
API_AVAILABLE(macos(10.7), ios(4.3))
DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NONNULL3 DISPATCH_NOTHROW
void
dispatch_barrier_sync_f(dispatch_queue_t queue,
    void *_Nullable context,
    dispatch_function_t work);


group.h中的相關(guān)函數(shù)

/*!
 * @function dispatch_group_async_f
 *
 * @abstract
 * Submits a function to a dispatch queue and associates the block with the
 * given dispatch group.
 *
 * @discussion
 * See dispatch_group_async() for details.
 *
 * @param group
 * A dispatch group to associate with the submitted function.
 * The result of passing NULL in this parameter is undefined.
 *
 * @param queue
 * The dispatch queue to which the function will be submitted for asynchronous
 * invocation.
 *
 * @param context
 * The application-defined context parameter to pass to the function.
 *
 * @param work
 * The application-defined function to invoke on the target queue. The first
 * parameter passed to this function is the context provided to
 * dispatch_group_async_f().
 */
API_AVAILABLE(macos(10.6), ios(4.0))
DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NONNULL2 DISPATCH_NONNULL4
DISPATCH_NOTHROW
void
dispatch_group_async_f(dispatch_group_t group,
    dispatch_queue_t queue,
    void *_Nullable context,
    dispatch_function_t work);
/*!
 * @function dispatch_group_notify_f
 *
 * @abstract
 * Schedule a function to be submitted to a queue when all the blocks
 * associated with a group have completed.
 *
 * @discussion
 * See dispatch_group_notify() for details.
 *
 * @param group
 * The dispatch group to observe.
 * The result of passing NULL in this parameter is undefined.
 *
 * @param context
 * The application-defined context parameter to pass to the function.
 *
 * @param work
 * The application-defined function to invoke on the target queue. The first
 * parameter passed to this function is the context provided to
 * dispatch_group_notify_f().
 */
API_AVAILABLE(macos(10.6), ios(4.0))
DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NONNULL2 DISPATCH_NONNULL4
DISPATCH_NOTHROW
void
dispatch_group_notify_f(dispatch_group_t group,
    dispatch_queue_t queue,
    void *_Nullable context,
    dispatch_function_t work);



就拿這幾個有代表性的函數(shù)當(dāng)做例子吧。

這些函數(shù)有一個特點,去掉后面的 _f 就是我們平時常用的GCD以block回調(diào)的方式處理多線程的函數(shù)。而加上了這個 _f 之后它們會變成什么鬼呢?

看看官方文檔中的解釋,以 dispatch_async_f 為例:

/*!
 * @function dispatch_async_f
 *
 * @abstract
 * Submits a function for asynchronous execution on a dispatch queue.
 *
 * @discussion
 * See dispatch_async() for details.
 *
 * @param queue
 * The target dispatch queue to which the function is submitted.
 * The system will hold a reference on the target queue until the function
 * has returned.
 * The result of passing NULL in this parameter is undefined.
 *
 * @param context
 * The application-defined context parameter to pass to the function.
 *
 * @param work
 * The application-defined function to invoke on the target queue. The first
 * parameter passed to this function is the context provided to
 * dispatch_async_f().
 * The result of passing NULL in this parameter is undefined.
 */

文檔中 @abstract 摘要部分寫到:

Submits a function for asynchronous execution on a dispatch queue.

就是說“提交一個在分派隊列上異步執(zhí)行的函數(shù)”。再看 @discussion 部分:

See dispatch_async() for details.

哈哈,讓我們到 dispatch_async() 去找更多的細節(jié)。接下來再看 @param context

The application-defined context parameter to pass to the function.

要傳遞給函數(shù)的應(yīng)用程序定義的上下文參數(shù),也就是我們自定義的要傳給執(zhí)行函數(shù)的參數(shù)。最后看看 @param work

The application-defined function to invoke on the target queue. The first parameter passed to this function is the context provided to dispatch_async_f().
?
The result of passing NULL in this parameter is undefined.

要在目標隊列上調(diào)用的自定義函數(shù)。傳入到函數(shù)的第一個參數(shù)是提供給 dispatch_async_f() 函數(shù)的上下文參數(shù)。如果這個參數(shù)傳NULL,那么結(jié)果是未定義的。

綜上所述,我們其實已經(jīng)可以看得出這些函數(shù)是干嘛的了。就是以函數(shù)執(zhí)行具體任務(wù)的方式替代GCD以Block執(zhí)行具體任務(wù)的方式。

下面我 給出兩個具體代碼案例,僅供參考!

- (void)viewDidLoad {
    [super viewDidLoad];
    
    dispatch_async_f(dispatch_get_main_queue(), (void *)123, (void *)async_func);
    
    dispatch_sync_f(dispatch_get_global_queue(0, DISPATCH_QUEUE_PRIORITY_DEFAULT), (void *)456, (void *)sync_func);
}

void async_func(int param) {
    printf("我是給async添加的C語言函數(shù):%d",param);
    printf("\n");
}

void sync_func(int param) {
    printf("我是給sync添加的C語言函數(shù):%d",param);
    printf("\n");
}



控制臺:

ok,寫這篇博客就是為了記錄一下當(dāng)初的好奇心,為什么會有很多CGD函數(shù)有個后面帶 _f 的版本,算是一篇筆記了。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 伴隨著黏黏身子和疲憊的眼睛,思緒踟躕游走…… 毒辣的太陽老頭和說走就走說來就來的烈雨大叔在笑,笑我心高氣傲...
    刺猬不刺猬閱讀 9,711評論 5 3
  • 又是一年畢業(yè)季,很多大學(xué)生步入工作崗位。面對第一份工作,我們該如何選擇呢?很多人都很迷茫。是要錢還是要經(jīng)驗,可能直...
    安伶兒閱讀 777評論 0 1
  • 16-8-17 歸來(安娜卡列寧娜) 作者:誰心所欲 安娜重生回到臨死前在車站徘徊的時刻,她后悔了。 和伏倫斯基分...
    海水藍閱讀 780評論 0 1
  • Carthage 使用流程 創(chuàng)建一個文件,名為 Cartfile, 在其中列出項目中需要使用到的 framewor...
    揚揚揚閱讀 2,338評論 1 2
  • 3.2第二次活動如期舉行,在之前就在想這期又是什么主題呢?聆聽
    麻花_5612閱讀 296評論 0 0

友情鏈接更多精彩內(nèi)容