Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find all threads of a process given process id

I am trying to find if there is a better way to find all thread ids that belong to the current process. It looks like using CreateToolhelp32Snapshot with TH32CS_SNAPTHREAD and iterating over the threads to check if the thread's process id is equal to the current process id, is a solution but i want to avoid iterating all the running threads. I just want to iterate over the threads that belong to a given process. Please let me know if there is an API that is fast and simple. I need to do it in c++.

Thanks, Abhinay.

like image 780
Abhinay K Reddyreddy Avatar asked Jan 24 '12 22:01

Abhinay K Reddyreddy


1 Answers

If the "current process" is one you've written, you can take advantage of the fact that DllMain functions get called any time a thread is added or terminated with reason codes of DLL_THREAD_ATTACH and DLL_THREAD_DETACH. It's simple to then keep your own list.

like image 188
DougN Avatar answered Oct 05 '22 01:10

DougN