4

I'm trying to profile my code using the Android Profiler. The problem is naming my C++ threads, I've tried using:

pthread_setname_np(pthread_self(), "MyThread");

But it doesn't show the specified name. How can I name my C++ thread on Android?

Oren Bengigi
  • 994
  • 9
  • 17

1 Answers1

1
  1. Obtain a reference to your JavaVM:
JavaVM* jvm;
env->GetJavaVM(&jvm);
  1. Set the name while attaching your thread to the JVM:
std::thread myThread([jvm](){
  JNIEnv* myNewEnv;
  JavaVMAttachArgs args;
  args.version = JNI_VERSION_1_6;
  args.name = "Fancy Thread";
  args.group = NULL;
  jvm->AttachCurrentThread((JNIEnv**)&myNewEnv, &args);

  while(1){
   // ....
  }

});
  1. You will now see the right thread name in Android Studio debugger.
Awsed
  • 9,094
  • 5
  • 26
  • 25