Search code examples
androidmultithreadingc++11android-ndkjava-native-interface

Why does creating a C++11 thread cause a Fatal Signal?


I want to create a C++11 thread that runs indefinitely, after a JNI call has been made. Why does this generate a Fatal Signal?

#include <thread>

static void teste()
{
    while(true)
        LOGI("IN TEST");
}

JNIEXPORT void Java_blahblah(JNIEnv *javaEnvironment, jobject self)
{
    std::thread t(teste);
    //t.join(); //I don't want to join it here.
}

I don't need the C++11 thread to call JNI or anything like that.


Solution

  • According to this answer, the destructor of the thread will call std::terminate if the thread is still joinable at time of destruction.

    If you do not want to join the thread, you can fix this by detaching the thread instead.

    std::thread t(teste).detach();