the*_*101 6 c++ java-native-interface multithreading android
我需要从android应用程序的本机部分启动一个新线程.它必须在JNI中声明和定义.有人知道我该怎么办?如果有人发布这个例子,我会非常高兴.
Ste*_*e M 10
最简单的方法是使用C++ 11线程类.请参阅此主题,了解如何使用Android NDK启用C++ 11.如果您在使线程类工作时遇到问题,请参阅此文章.然后你可以像这样使用它:
#include <thread> // std::thread
void foo()
{
// do stuff...
}
void bar(int x)
{
// do stuff...
}
JNIEXPORT void JNICALL
Java_org_testjni_android_Game_someFunction(JNIEnv * env, jobject obj)
{
std::thread first (foo); // spawn new thread that calls foo()
std::thread second (bar,0); // spawn new thread that calls bar(0)
//main, foo and bar now execute concurrently
// synchronize threads:
first.join(); // pauses until first finishes
second.join(); // pauses until second finishes
}
Run Code Online (Sandbox Code Playgroud)
如果你不能使用C++ 11,只需使用pthread(POSIX线程),它没有太大的不同,除了它像旧的C:
#include <pthread.h>
//This function will be called from a thread
void *call_from_thread(void *) {
//do stuff
return NULL;
}
JNIEXPORT void JNICALL
Java_org_testjni_android_Game_someFunction(JNIEnv * env, jobject obj)
{
pthread_t t;
//Launch a thread
pthread_create(&t, NULL, call_from_thread, NULL);
//Join the thread with the main thread
pthread_join(t, NULL);
}
Run Code Online (Sandbox Code Playgroud)
这里有一些关于在Android上使用POSIX线程的更多信息.
此外,如果要在任何线程中使用它,而不是调用JNI函数的线程,您还需要查找如何将JNIEnv指针绑定到当前线程.来自JNI规范:
创建VM
JNI_CreateJavaVM()函数加载并初始化Java VM并返回指向JNI接口指针的指针.调用JNI_CreateJavaVM()的线程被认为是主线程.
附加到VM
JNI接口指针(JNIEnv)仅在当前线程中有效.如果另一个线程需要访问Java VM,它必须首先调用AttachCurrentThread()以将自身附加到VM并获取JNI接口指针.一旦连接到VM,本机线程就像在本机方法中运行的普通Java线程一样工作.本机线程保持连接到VM,直到它调用DetachCurrentThread()来分离自身.
附加的线程应该有足够的堆栈空间来执行合理的工作量.每个线程的堆栈空间分配是特定于操作系统的.例如,使用pthreads,可以在pthread_create的pthread_attr_t参数中指定堆栈大小.
从VM分离
附加到VM的本机线程必须在退出之前调用DetachCurrentThread()以分离自身.如果调用堆栈上有Java方法,则线程无法分离.
| 归档时间: |
|
| 查看次数: |
11580 次 |
| 最近记录: |