[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 254
  • Last Modified:

Determining thread count

Friends,


How to know, no of threads that are executing currently

Currently I am taking one global variable and incrementing and decrementing the variable  using following methods in determining the thread count



::InterlockedIncrement(&threadCounter);
::InterlockedDecrement(&threadCounter);


Rather using above idea, I want the idea similar to this where I can know direct thread count
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Thread.html#activeCount()

thanks
sudhakar



0
sudhakar_koundinya
Asked:
sudhakar_koundinya
  • 4
  • 2
2 Solutions
 
jkrCommented:
You can do that by providing a simple DLL that is loaded before you use any of these APIs. All you have to do is to provide a 'DllMain()' which will get notified about the threads:

unsigned int unThreadCount = 0;

extern "C"
unsigned int GetThreadCount () { return unThreadCount;}

// DllMain() is the entry-point function for this DLL.
BOOL
WINAPI
DllMain ( HINSTANCE, DWORD dwReason, LPVOID) {

   DWORD dwTID = 0;

   switch ( dwReason) {
 
       // The attached process creates a new thread.
 
       case DLL_THREAD_ATTACH:

            unThreadCount++;

            break;
 
       // The thread of the attached process terminates.

        case DLL_THREAD_DETACH:
 
            unThreadCount++;
           
           break;
 
       default:
            break;
    }
 
   return TRUE;
}
0
 
Jaime OlivaresCommented:
As far as I know, you can't get a direct count of threads for current process in C++, so InterlockedIncrement() is a good choice.
0
 
jkrCommented:
>> As far as I know, you can't get a direct count of threads for current process in C++

See the above code.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
jkrCommented:
BTW, a more complicated solution would be to 'CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, GetCurrentProcessId());' and counting the threads, but...
0
 
Jaime OlivaresCommented:
>>See the above code.
This is not a direct thread count, just a trick similar to author proposal.
Your second alternative has the same flavour.

Java language, which I don't like to use by the way, mantains an internal ready-made thread list and counter.
Also you can have a similar counter with .net environment with System.Threading.ThreadPool class (have a look to ).
But I have not seen a "GetThreadCount" function in WinAPI.

0
 
jkrCommented:
>>This is not a direct thread count, just a trick

I tend to object. You might want to explain why this does not work rather than only claiming that.
Just in case I haven't been clear, I was speaking of an implicitly loaded DLL. And I am really courious about the reason why a 'snapshot' should be a 'trick'.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now