Android NDK的pthread学习笔记
java多线程
在java用new Thread新建线程,cpp文件中的全局变量在所有线程中都是用同一个变量。函数是分开运行的。
new Thread(new Runnable() {
public void run() {
testPthread("Tom");
}
}).start();
new Thread(new Runnable() {
public void run() {
testPthread("Mary");
}
}).start();
log
04-26 11:18:17.000 30848-30881/com.obarong.testpthread E/hello: I am hello in hello.cpp from Mary. num=2
04-26 11:18:17.030 30848-30880/com.obarong.testpthread E/hello: I am hello in hello.cpp from Tom. num=2
04-26 11:18:18.700 30848-30881/com.obarong.testpthread E/hello: I am hello in hello.cpp from Mary. num=3
04-26 11:18:18.730 30848-30880/com.obarong.testpthread E/hello: I am hello in hello.cpp from Tom. num=3
04-26 11:18:20.380 30848-30881/com.obarong.testpthread E/hello: I am hello in hello.cpp from Mary. num=4
04-26 11:18:20.420 30848-30880/com.obarong.testpthread E/hello: I am hello in hello.cpp from Tom. num=4
native多线程
如果在C++里面开线程,会有错误
// new Thread(new Runnable() {
// public void run() {
testPthread("Tom");
// }
// }).start();
// new Thread(new Runnable() {
// public void run() {
testPthread("Mary");
// }
// }).start();
native-lib.cpp
void *printHello(void *args) {
LOGE("enter printHello");
LOGE("(char *)args=%s", (char *) args);
char temp[100];
strcpy(temp, (char *) args);
sayhello(temp);
// 释放线程资源
pthread_attr_destroy(&pthreadAttr);
pthread_exit(NULL); //!!!自动释放
}
extern "C"
JNIEXPORT void JNICALL
Java_com_obarong_testpthread_MainActivity_testPthread(JNIEnv *env, jobject instance, jstring str_) {
const char *str = env->GetStringUTFChars(str_, 0);
char temp[100];
strcpy(temp, str);
pthread_t pthread;
pthread_attr_init(&pthreadAttr); //初始化线程属性
pthread_attr_setdetachstate(&pthreadAttr, PTHREAD_CREATE_DETACHED);
// 创建线程
int err = pthread_create(&pthread, &pthreadAttr, printHello, (void *) temp);
if (err != 0)
LOGE("can't create thread: %s\n", strerror(err));
// sayhello(str);
env->ReleaseStringUTFChars(str_, str);
}
log输出乱码
04-26 11:15:39.930 29620-29668/com.obarong.testpthread E/hello: I am hello in hello.cpp from ����TA. num=3
04-26 11:15:39.980 29620-29669/com.obarong.testpthread E/hello: I am hello in hello.cpp from Mary. num=3
04-26 11:15:41.600 29620-29668/com.obarong.testpthread E/hello: I am hello in hello.cpp from ����TA. num=4
04-26 11:15:41.650 29620-29669/com.obarong.testpthread E/hello: I am hello in hello.cpp from Mary. num=4
04-26 11:15:43.260 29620-29668/com.obarong.testpthread E/hello: I am hello in hello.cpp from ����TA. num=5
04-26 11:15:43.320 29620-29669/com.obarong.testpthread E/hello: I am hello in hello.cpp from Mary. num=5
问题出在内存分配上,用malloc
分配内存
char *temp = (char *) malloc(strlen(str) + 1);
log输出正常
04-27 19:54:44.750 30943-30975/com.obarong.testpthread E/hello: I am hello in hello.cpp from Tom. num=1
04-27 19:54:44.780 30943-30976/com.obarong.testpthread E/hello: I am hello in hello.cpp from Mary. num=1
04-27 19:54:48.120 30943-30976/com.obarong.testpthread E/hello: I am hello in hello.cpp from Mary. num=2
04-27 19:54:48.130 30943-30975/com.obarong.testpthread E/hello: I am hello in hello.cpp from Tom. num=2
参考
NativeDemo:
Android NDK — Native 线程 pthread - 简书
https://www.jianshu.com/p/34d88df0cfe0
Android NDK — Java 与 C/C++ 的互相调用 - 简书
https://www.jianshu.com/p/cdfcfc7489bb
pthread_create_百度百科
https://baike.baidu.com/item/pthread_create
Android NDK 原生 API | Android NDK | Android Developers
https://developer.android.google.cn/ndk/guides/stable_apis