没有街区的Grand Central Dispatch [英] Grand Central Dispatch without blocks

查看:135
本文介绍了没有街区的Grand Central Dispatch的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在没有块的情况下使用GCD?有没有办法使用_f变体使用GCD,因为mikeash在他的发布。我四处搜寻,没有证据证明任何一方。是可能的还是不可能的。

Is it possible to use GCD without blocks? Is there a way to use GCD using _f variant as mikeash says in his post. I searched around and there is no proof for either sides. is it possible or impossible.

如果可行,请举个例子。

If its doable please give an example.

/ Selvin

推荐答案

当然有可能!通过 _f 变体Mike只是表示 GCD 的函数 _f 后缀。它们是通常的 GCD 函数的替代品,但可以接受用户定义的函数作为参数而不是块。有很多:

Of course it is possible! By _f variants Mike just mean set of GCD functions with _f suffix. They are alternatives for usual GCD functions but can accept a user defined function as a parameter instead of blocks. There are plenty of them:

dispatch_async_f
dispatch_sync_f
dispatch_after_f
dispatch_apply_f
dispatch_group_async_f
dispatch_group_notify_f
dispatch_set_finalizer_f
dispatch_barrier_async_f
dispatch_barrier_sync_f
dispatch_source_set_registration_handler_f
dispatch_source_set_cancel_handler_f
dispatch_source_set_event_handler_f

他们接受 dispatch_function_t 参数(而不是通常的 dispatch_block_t )定义如下:

typedef void(* dispatch_function_t)(void *)

如您所见,由于 * void 指针,它可以接受任何用户参数和函数。所以你甚至可以使用没有参数的函数 dispatch_function_t - 你可以像这样写一个包装器函数:

They accept dispatch_function_t parameter (instead of usual dispatch_block_t) which is defined as follows:

typedef void (*dispatch_function_t)(void*).

As you see it can accept any user parameter and also a function because of *void pointer. So you can even use dispatch_function_t with function that have no arguments - you can just write a wrapper function like so:

void func(void) {
  //do any calculations you want here
    }
void wrapper_function(void*) { func(); }
dispatch_async_f(queue, 0, &wrapper_function);

或者将函数指针作为参数传递。或者相反,您可以使用 _f GCD函数的变体和用户定义的函数,这些函数可以通过varargs(可变函数函数)接受任意数量的参数 - 只需编写一个函数包装器它也如上所述。如你所见 _f 函数是一个强大的机制,你不仅限于没有GCD参数的块,而是可以使用常用函数。

Or pass a function pointer as a parameter. Or on the contrary you can use _f variants of GCD functions with user defined functions which can accept any number of arguments via the varargs (variadic functions) - just write a function wrapper for it also as above. As you see _f functions is rather a powerful mechanism and you are not limited only with blocks without parameters for GCD but can use usual functions.

这篇关于没有街区的Grand Central Dispatch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆