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
的版本闹丐,算是一篇筆記了横殴。