如何张贴code是从C ++中一个单独的线程在Android主线程上运行? [英] How do I post code to be run on the Android main thread from a separate thread in C++?

查看:152
本文介绍了如何张贴code是从C ++中一个单独的线程在Android主线程上运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在后台一个单独的线程用C ++运行,我希望它能够张贴code要在一个已经运行的android.os.Looper(例如主线程),另一个线程运行。通过'后',​​我的意思是查看#后一个类似于其中a 的Runnable 排入要在事件中运行循环。将要执行的code也是用C ++编写。

I have a separate thread running in C++ in the background and I want it to be able to post code to be run on another thread that's already running an android.os.Looper (e.g. the main thread). By 'post', I mean something akin to View#post where a Runnable is enqueued to be run on the event loop. The code that would be executed is also written in C++.

我发现ALooper API(<一个href=\"http://developer.android.com/ndk/reference/group___looper.html\">http://developer.android.com/ndk/reference/group___looper.html),但该文档都不是很大,它的我不清楚是否获得与目标线程关联的ALooper,增加一个FD和信令那会使我的code维持事件队列正确的顺序相对于其他入队的Runnable

I found the ALooper API (http://developer.android.com/ndk/reference/group___looper.html), but the docs aren't great and it's unclear to me whether getting the ALooper associated with the destination thread, adding another FD, and signaling it will make my code maintain correct ordering in the event queue with respect to the other enqueued Runnables.

我倒是preFER不是必须去通过Java,并得到一个处理程序,等等 - 似乎只是因为两者的code我试图运行,code这是不必要的张贴在C ++中。

I'd prefer not to have to go through Java and get a Handler, etc. -- it just seems unnecessary since both the code I'm trying to run and the code that's posting it are in c++.

推荐答案

一个线程只能有一条环线与它相关联,一个活套只有一个消息队列,所以混合Java和本机的回调将保持排序。

A thread can only have one Looper associated with it, a Looper has only one message queue, so mixing Java and native callbacks will maintain ordering.

就这样,我不认为在Android的任何合同义务今天后()有保证,特别为了执行,即。

With that, I don't think there is any contractual obligation in Android today that post() is guaranteed to execute in particular order, i.e.

getHandler().post(new Runnable() {
    @Override
    public void run() {
        mTextView.setText("first");
    }
});
getHandler().post(new Runnable() {
    @Override
    public void run() {
        mTextView.setText("second");
    }
});

不保证正式离开 mTextView 显示的第二的。绝对没有什么是一成不变的,当两个发表,则来自不同的线程发出,或延迟。

is not formally guaranteed to leave mTextView displaying second. Definitely nothing is set in stone when two posts are issued from different threads, or delayed.

您可以找到一个<一个href=\"https://github.com/Himmele/My-Blog-Repository/tree/master/Google%20Android/Android%20Messaging%20and%20Concurrency\"相对=nofollow> Android的消息和并发原生code开发框架的desctibed在一个伟大的博客文章

You can find an Android messaging and concurrency framework for native code development desctibed in a great blog post.

下面是必要的证明。堆栈跟踪下面的一个无关的问题工作时收到:

Here is the required proof. The stacktrace below was received while working on an unrelated problem:

A/art: art/runtime/check_jni.cc:65]   native: #00 pc 0000484c  /system/lib/libbacktrace_libc++.so (UnwindCurrent::Unwind(unsigned int, ucontext*)+23)
A/art: art/runtime/check_jni.cc:65]   native: #01 pc 00003031  /system/lib/libbacktrace_libc++.so (Backtrace::Unwind(unsigned int, ucontext*)+8)
A/art: art/runtime/check_jni.cc:65]   native: #02 pc 002441f9  /system/lib/libart.so (art::DumpNativeStack(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, int, char const*, art::mirror::ArtMethod*)+68)
A/art: art/runtime/check_jni.cc:65]   native: #03 pc 002285a1  /system/lib/libart.so (art::Thread::Dump(std::__1::basic_ostream<char, std::__1::char_traits<char> >&) const+144)
A/art: art/runtime/check_jni.cc:65]   native: #04 pc 000afe9b  /system/lib/libart.so (art::JniAbort(char const*, char const*)+582)
A/art: art/runtime/check_jni.cc:65]   native: #05 pc 000b05d1  /system/lib/libart.so (art::JniAbortF(char const*, char const*, ...)+60)
A/art: art/runtime/check_jni.cc:65]   native: #06 pc 000b299d  /system/lib/libart.so (art::ScopedCheck::Check(bool, char const*, ...) (.constprop.129)+672)
A/art: art/runtime/check_jni.cc:65]   native: #07 pc 000bab87  /system/lib/libart.so (art::CheckJNI::CallVoidMethodV(_JNIEnv*, _jobject*, _jmethodID*, std::__va_list)+50)
A/art: art/runtime/check_jni.cc:65]   native: #08 pc 00060817  /system/lib/libandroid_runtime.so (???)
A/art: art/runtime/check_jni.cc:65]   native: #09 pc 000a5b29  /system/lib/libandroid_runtime.so (???)
A/art: art/runtime/check_jni.cc:65]   native: #10 pc 00010fd7  /system/lib/libutils.so (android::Looper::pollInner(int)+482)
A/art: art/runtime/check_jni.cc:65]   native: #11 pc 00011081  /system/lib/libutils.so (android::Looper::pollOnce(int, int*, int*, void**)+92)
A/art: art/runtime/check_jni.cc:65]   native: #12 pc 0007fbe5  /system/lib/libandroid_runtime.so (android::NativeMessageQueue::pollOnce(_JNIEnv*, int)+22)
A/art: art/runtime/check_jni.cc:65]   native: #13 pc 00051b8b  /system/framework/arm/boot.oat (Java_android_os_MessageQueue_nativePollOnce__JI+102)
A/art: art/runtime/check_jni.cc:65]   at android.os.MessageQueue.nativePollOnce(Native method)
A/art: art/runtime/check_jni.cc:65]   at android.os.MessageQueue.next(MessageQueue.java:143)
A/art: art/runtime/check_jni.cc:65]   at android.os.Looper.loop(Looper.java:122)
A/art: art/runtime/check_jni.cc:65]   at android.app.ActivityThread.main(ActivityThread.java:5411)
A/art: art/runtime/check_jni.cc:65]   at java.lang.reflect.Method.invoke!(Native method)
A/art: art/runtime/check_jni.cc:65]   at java.lang.reflect.Method.invoke(Method.java:372)
A/art: art/runtime/check_jni.cc:65]   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:916)
A/art: art/runtime/check_jni.cc:65]   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:709)

这篇关于如何张贴code是从C ++中一个单独的线程在Android主线程上运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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