About c++ : How-to-print-thread-id-of-all-threads-a-process-on-linux-has
Question Detail
I know how to print thread id while executing in the context of a thread, but I would like to print all thread ids that a process has spawned. I need this to correlate with strace output for debugging.
How to get current thread id:
pid_t x = syscall(__NR_gettid);
Question Answer
From https://unix.stackexchange.com/a/901/134332
For each process, a lot of information is available in /proc/12345 where 12345 is the process ID. Information on each thread is available in /proc/12345/task/67890 where 67890 is the kernel thread ID. This is where ps, top and other tools get their information.
……………………………………………………
You can read the virtual /proc filesystem. Iterate over the dirnames in /proc/self/task.
if(DIR* dir = opendir(“/proc/self/task”)) {
while (dirent* entry = readdir(dir))
if (entry->d_name[0] != ‘.’)
std::cout << entry->d_name;
closedir(dir);
}