NDK/JNI: identifying current thread

匿名 (未验证) 提交于 2019-12-03 08:46:08

问题:

In a JNI native method, is there a way to know the ID of the current thread without calling Java back? Thread-local storage would also work.

EDIT: pthread.h is present in the NDK include folder... Does anyone know if Java threads correspond to POSIX ones in the NDK implementation?

回答1:

Which ID are you interested in? A Dalvik thread dump includes this:

"main" prio=5 tid=1 TIMED_WAIT   | group="main" sCount=1 dsCount=0 obj=0x40017730 self=0x12798   | sysTid=3167 nice=0 sched=0/0 cgrp=default handle=-2146114456   | schedstat=( 358850000 275073000 869 ) utm=23 stm=12 core=0

"tid" is the VM's ID. "handle" is the pthread_t. "sysTid" is the result of gettid() (the Linux process ID).

The libcore thread ID (obtained from java.lang.Thread.getId()) is not shown.

(You can obtain the above with "adb shell kill -3 ". The output goes to a common file, defined by the dalvik.vm.stack-trace-file property -- usually /data/anr/traces.txt, but it varies by device.)

EDIT: Every Dalvik VM thread is a Linux pthread. The gettid() syscall will give you a unique ID for each thread. Also, you can add identifying information to TLS in java.lang.Thread or pthread_key.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!