Determining thread count


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


Rather using above idea, I want the idea similar to this where I can know direct thread count


LVL 14
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.
DllMain ( HINSTANCE, DWORD dwReason, LPVOID) {

   DWORD dwTID = 0;

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


       // The thread of the attached process terminates.

        case DLL_THREAD_DETACH:
   return TRUE;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jaime OlivaresSoftware ArchitectCommented:
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.
>> As far as I know, you can't get a direct count of threads for current process in C++

See the above code.
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

BTW, a more complicated solution would be to 'CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, GetCurrentProcessId());' and counting the threads, but...
Jaime OlivaresSoftware ArchitectCommented:
>>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.

>>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'.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.