windows api: how to find active thread count in current process?
I have written a server based on boost::asio in windows platform. I want to log currently active thread count of my application at regular intervals.
I can see the thread count for my application in windows task manager's process view in Threads column. is there a windo开发者_高级运维ws api to get the same?
after futile googling for sometime i thought its best to seek advice from SO.
Raymond Chen has the answer, based on the Tool Help Library.
For the sake of completeness, his sample code is as follows:
#include <stdio.h>
#include <windows.h>
#include <tlhelp32.h>
int __cdecl main(int argc, char **argv)
{
HANDLE h = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
if (h != INVALID_HANDLE_VALUE) {
THREADENTRY32 te;
te.dwSize = sizeof(te);
if (Thread32First(h, &te)) {
do {
if (te.dwSize >= FIELD_OFFSET(THREADENTRY32, th32OwnerProcessID) +
sizeof(te.th32OwnerProcessID)) {
printf("Process 0x%04x Thread 0x%04x\n",
te.th32OwnerProcessID, te.th32ThreadID);
}
te.dwSize = sizeof(te);
} while (Thread32Next(h, &te));
}
CloseHandle(h);
}
return 0;
}
精彩评论