After spending several hours debugging, I finally figure out how to solve the crash in my native code while calling pthread_exit(). The log showed “thread exiting, not yet detached (count=0)“.
Scenario:
I have an Android java code which will call my JNI (NDK) native code. In my native code, I create a pthread and in my pthread loop, I called AttachCurrentThread() to the JVM in the beginning of the loop.
When my java code finish some jobs, I need to stop the pthread and it crashed at the end of pthread loop.
Solution:
So, before I exit my pthread loop (before calling pthread_exit), I need to make sure I do call DetachCurrentThread from JVM.
could you provide me the java and cpp/c source of the pthread issue you mentioned above
By: bongo on May 12, 2011
at 9:43 pm
Hi bongo,
here is a pseudo code for this:
1. First, you create a pthread with pthread_create(&id, &attr, thread_func, this);
2. Second, in your thread_func()
void *thread_func(void *yourData)
{
jvm->AttachCurrentThread(...); //Important
whileloop
{
do_your_heavy_thing_in_thread();
and_maybe_call_your_java_method_from_c_here();
}
jvm->DetachCurrentThread(...); //Important
}
By: slworkthings on May 16, 2011
at 8:35 am