libDispatch在Android上提供主队列而不提供dispatch_main [英] libDispatch serving main queue without dispatch_main on Android

查看:112
本文介绍了libDispatch在Android上提供主队列而不提供dispatch_main的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android平台上使用libDispatch(GCD)开源. 因此,大多数复杂的耗时任务都是通过NDK(我正在使用libDispatch)完成的.

I am using libDispatch (GCD) opensource on Android platform. So, most of the complex time consuming tasks are being done through NDK (where i am using libDispatch).

对于某些电话,我正在使用dispatch_async(get_main_queue)...这就是问题要解决的地方... 我能够在并发队列中运行任务,但不能在主队列中运行任务. 由于这需要调用dispatch_main(),因此我们无法在此执行此操作,因为在这种情况下,Java线程将被阻塞. 因此,是否有可能在某个辅助线程上运行Java UI并挂接到dispatch_main()以便在此处提供dispatch_main_queue? 或者:是否需要继续通过JNI从JAva主UI线程提供main_queue?

For some calls, I am using dispatch_async(get_main_queue)...This is where the problem is coming... I am able to run tasks in the concurrent queues but not on main queue. Since this requires dispatch_main() to be called which we cannot do on here as Java thread will be blocked in that case. So, is it possible to run the Java UI on some secondary thread and hook the dispatch_main() to serve the dispatch_main_queue here? OR : Do I need to keep serving the main_queue from JAva main UI thread through JNI ?

推荐答案

查看_dispatch_main_queue_callback_4CF,您可以调用它来清空主队列.执行排队的操作后,它将像正常的明智函数一样返回,而不是像dispatch_main这样杀死线程.

Look into _dispatch_main_queue_callback_4CF which is the function you can call to drain the main queue. It will return like a normal sensible function after executing the queued operations, instead of killing the thread like dispatch_main.

请注意,您需要定期从Java UI线程(可能是每次迭代)中调用_dispatch_main_queue_callback_4CF.官方的Cocoa实现使用_dispatch_queue_wakeup_main(),它使用mach消息将主线程踢出任何睡眠状态,因此可以保证快速调用回调函数,但是您必须做一些工作来启用它并构建自己的libDispatch港口.实际上,在Android上,我认为您的应用处于活动状态时,主UI线程不会处于休眠状态,因此这不应该成为问题.

Note that you'll need to call _dispatch_main_queue_callback_4CF on a regular basis from your Java UI thread, possibly each iteration. The official Cocoa implementation uses _dispatch_queue_wakeup_main() which uses mach messages to kick the main thread out of any sleep states so it can guarantee the callback function is called quickly, but you'd have to do some work to enable that and build your own libDispatch port. In reality on Android I don't think the main UI thread is ever put to sleep while your app is active so it shouldn't be an issue.

libDispatch网站上的一张票证位于 https://libdispatch.macosforge.org/trac/ticket/38 _dispatch_main_queue_callback_4CF设置为公共功能.该票证被标记为已接受",但没有说明何时/何时发生.

There is a ticket open on the libDispatch site at https://libdispatch.macosforge.org/trac/ticket/38 to make _dispatch_main_queue_callback_4CF a public function. The ticket is marked "Accepted" but no word on if/when that will happen.

这篇关于libDispatch在Android上提供主队列而不提供dispatch_main的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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