在NDK开发中,使用pthread_create()创建子线程时,如果保持非主线程在运行时,使用pthread_join()函数会导致应用程序崩溃。为了解决这个问题,可以使用pthread_detach()函数将线程与主线程分离。这样,非主线程可以在后台运行,而主线程可以继续执行。以下是代码示例:
#include
void *myThread(void * arg)
{
return NULL;
}
void launch_thread()
{
pthread_t thread_id;
pthread_create(&thread_id, NULL, myThread, NULL);
// Detach the thread so that it can run independently
pthread_detach(thread_id);
}