I can enumerate tids of all threads of current process via /proc/self/task
as described here. How can I map these thread ids to std::thread::id
-s in case there are some threads created by libraries that I use?
For example, this program:
#include <iostream>
#include <thread>
#include <vector>
#include <errno.h>
#include <sched.h>
#include <sys/types.h>
#include <dirent.h>
int main()
{
auto get_thread_ids = [] () -> std::vector<int>
{
std::unique_ptr<DIR, int (*)(DIR*)> self_dir{opendir("/proc/self/task"), &closedir};
if (!self_dir)
return {};
std::vector<int> ret{};
struct dirent *entry = nullptr;
while ((entry = readdir(self_dir.get())) != nullptr)
{
if (entry->d_name[0] == '.')
continue;
ret.emplace_back(std::stoi(entry->d_name));
}
return ret;
};
std::cout << "main " << std::this_thread::get_id() << std::endl;
std::thread t{
[](){
std::cout << "thread " << std::this_thread::get_id() << std::endl;
std::this_thread::sleep_for(std::chrono::seconds{5});
}
};
for (const auto& i : get_thread_ids())
std::cout << "tid: " << i << std::endl;
t.join();
}
prints this:
main 140125847566144
tid: 31383
tid: 31384
thread 140125829990144
I want to be able to establish correspondence: 31383->140125847566144
, 31384->140125829990144
.