Android线程名称最大15字节
int pthread_setname_np(pthread_t thread, const char *name)
{
int fd, cs, status = 0;
char f[sizeof "/proc/self/task//comm" + 3*sizeof(int)];
size_t len;
if ((len = strnlen(name, 16)) > 15) return ERANGE;
if (thread == pthread_self())
return prctl(PR_SET_NAME, (unsigned long)name, 0UL, 0UL, 0UL) ? errno : 0;
snprintf(f, sizeof f, "/proc/self/task/%d/comm", thread->tid);
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
if ((fd = open(f, O_WRONLY|O_CLOEXEC)) < 0 || write(fd, name, len) < 0) status = errno;
if (fd >= 0) close(fd);
pthread_setcancelstate(cs, 0);
return status;
}
如果子线程未设置名称时,子线程继承父线程名称。
测试代码如下:
std::thread t([](){
pthread_setname_np(pthread_self(), "test1");
std::thread t1([](){
//pthread_setname_np(pthread_self(), "test2");
sleep(100);
});
std::thread t2([](){
//pthread_setname_np(pthread_self(), "test3");
sleep(100);
});
sleep(100);
});
sleep(100);