Archive for May, 2006

Thread Count in C++

May 19, 2006

If you want to get the thread count of your process, following code may help:

 DWORD pid = ::GetCurrentProcessId ();
 THREADENTRY32 entry;
 entry.dwSize = sizeof (THREADENTRY32 );
 int count;
 HANDLE handle = ::CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD , pid );
 BOOL enum_more = Thread32First ( handle, & entry);
 count = 0;
 while (1)
 {
   enum_more = Thread32Next ( handle, & entry);
   if ( !enum_more )
     break;
   if ( entry.th32OwnerProcessID == pid )
    count ++;
 }
 

After this code the *count* variable contains the thread count.