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);



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

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

看看官方文檔中的解釋偶垮,以 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.

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

See dispatch_async() for details.

哈哈,讓我們到 dispatch_async() 去找更多的細(xì)節(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.

要在目標(biāo)隊(duì)列上調(diào)用的自定義函數(shù)逝淹。傳入到函數(shù)的第一個(gè)參數(shù)是提供給 dispatch_async_f() 函數(shù)的上下文參數(shù)耕姊。如果這個(gè)參數(shù)傳NULL桶唐,那么結(jié)果是未定義的栅葡。

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

下面我 給出兩個(gè)具體代碼案例,僅供參考坯约!

- (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");
}



控制臺(tái):

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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子衫仑,更是在濱河造成了極大的恐慌梨与,老刑警劉巖,帶你破解...
    沈念sama閱讀 212,383評論 6 493
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件文狱,死亡現(xiàn)場離奇詭異粥鞋,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)瞄崇,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,522評論 3 385
  • 文/潘曉璐 我一進(jìn)店門呻粹,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人苏研,你說我怎么就攤上這事等浊。” “怎么了摹蘑?”我有些...
    開封第一講書人閱讀 157,852評論 0 348
  • 文/不壞的土叔 我叫張陵凿掂,是天一觀的道長。 經(jīng)常有香客問我纹蝴,道長庄萎,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,621評論 1 284
  • 正文 為了忘掉前任塘安,我火速辦了婚禮糠涛,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘兼犯。我一直安慰自己忍捡,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,741評論 6 386
  • 文/花漫 我一把揭開白布切黔。 她就那樣靜靜地躺著砸脊,像睡著了一般。 火紅的嫁衣襯著肌膚如雪纬霞。 梳的紋絲不亂的頭發(fā)上凌埂,一...
    開封第一講書人閱讀 49,929評論 1 290
  • 那天,我揣著相機(jī)與錄音诗芜,去河邊找鬼瞳抓。 笑死,一個(gè)胖子當(dāng)著我的面吹牛伏恐,可吹牛的內(nèi)容都是我干的孩哑。 我是一名探鬼主播,決...
    沈念sama閱讀 39,076評論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼翠桦,長吁一口氣:“原來是場噩夢啊……” “哼横蜒!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,803評論 0 268
  • 序言:老撾萬榮一對情侶失蹤丛晌,失蹤者是張志新(化名)和其女友劉穎鹰霍,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體茵乱,經(jīng)...
    沈念sama閱讀 44,265評論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡茂洒,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,582評論 2 327
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了瓶竭。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片督勺。...
    茶點(diǎn)故事閱讀 38,716評論 1 341
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖斤贰,靈堂內(nèi)的尸體忽然破棺而出智哀,到底是詐尸還是另有隱情,我是刑警寧澤荧恍,帶...
    沈念sama閱讀 34,395評論 4 333
  • 正文 年R本政府宣布瓷叫,位于F島的核電站,受9級特大地震影響送巡,放射性物質(zhì)發(fā)生泄漏摹菠。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 40,039評論 3 316
  • 文/蒙蒙 一骗爆、第九天 我趴在偏房一處隱蔽的房頂上張望次氨。 院中可真熱鬧,春花似錦摘投、人聲如沸煮寡。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,798評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至打月,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間坐儿,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,027評論 1 266
  • 我被黑心中介騙來泰國打工专钉, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留挑童,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 46,488評論 2 361
  • 正文 我出身青樓跃须,卻偏偏與公主長得像,于是被迫代替她去往敵國和親娃兽。 傳聞我的和親對象是個(gè)殘疾皇子菇民,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,612評論 2 350

推薦閱讀更多精彩內(nèi)容