Determining thread count

Posted on 2004-11-13
Last Modified: 2013-11-20

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


Question by:sudhakar_koundinya
    LVL 86

    Accepted Solution

    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;
    LVL 55

    Assisted Solution

    by:Jaime Olivares
    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.
    LVL 86

    Expert Comment

    >> As far as I know, you can't get a direct count of threads for current process in C++

    See the above code.
    LVL 86

    Expert Comment

    BTW, a more complicated solution would be to 'CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, GetCurrentProcessId());' and counting the threads, but...
    LVL 55

    Expert Comment

    by:Jaime Olivares
    >>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.

    LVL 86

    Expert Comment

    >>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'.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Suggested Solutions

    Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
    Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now