开发者

NDK/JNI: identifying current thread

In a JNI native method, is there a way to know the ID of the curren开发者_StackOverflowt 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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜