TestPthread

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值