Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6111905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:41:03+00:00 2026-05-23T14:41:03+00:00

On Windows, you can call SetProcessAffinityMask for a process and SetThreadAffinityMask for a thread.

  • 0

On Windows, you can call SetProcessAffinityMask for a process and SetThreadAffinityMask for a thread. However, Windows only appears to expose GetProcessAffinityMask and not a similar API for individual threads of a process.

I have a multi-threaded program that binds individual threads to processors at run-time. As I run it, I’d like to (externally) query which threads are running on which processors to make sure it’s working correctly. I’ve written a small command-line utility to do this. But I can’t seem to find a means of finding which processor(s) or core(s) an individual thread is bound to.

This apparently must be possible; I’ve seen descriptions online of the adplus debugging utility being able to show pstack-like output to show thread affinity. And Process Explorer shows a Threads tab on multi-processor machines that shows the “Ideal Processor” of a thread.

Does anyone know how to query this piece of information?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T14:41:04+00:00Added an answer on May 23, 2026 at 2:41 pm

    You can do it with two calls to SetThreadAffinityMask. This function returns the original affinity mask for the passed thread handle.

    So… do one call with a mask that sets affinity to one CPU, and then do a second call to restore the original mask.

    Here is complete C/C++ source code including error checking:

    DWORD GetThreadAffinityMask(HANDLE thread)
    {
        DWORD mask = 1;
        DWORD old = 0;
    
        // try every CPU one by one until one works or none are left
        while(mask)
        {
            old = SetThreadAffinityMask(thread, mask);
            if(old)
            {   // this one worked
                SetThreadAffinityMask(thread, old); // restore original
                return old;
            }
            else
            {
                if(GetLastError() != ERROR_INVALID_PARAMETER)
                    return 0; // fatal error, might as well throw an exception
            }
            mask <<= 1;
        }
    
        return 0;
    }
    

    This code probes one CPU at a time, until setting affinity works (in this case we now know the original mask!) or until the initial 1 has been shifted out of the DWORD. If a CPU is asked that is not available, the function fails with ERROR_INVALID_PARAMETER, and we just try the next one. Usually the first CPU will just work, so it’s reasonably efficient.

    If the function fails with anything other than ERROR_INVALID_PARAMETER, it means that we either don’t have sufficient access rights to the handle, or the system is having some real problems because it can’t fulfill our request. Therefore it doesn’t make sense to continue in this case.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know I can call the GetVersionEx Win32 API function to retrieve the Windows
I am looking for a Windows API that I can call from Delphi 2010
How exactly using VB6 can I can call any Windows shell command as you
On Windows I can do CreateProcess(..., CREATE_NEW_CONSOLE, ...) and my child process (which is
Is there an implementation of a Windows service that can call custom .NET code
If my java app is running under Windows I can call Runtime.getRuntime().exec (rundll32 SHELL32.DLL,ShellExec_RunDLL
Is there a Win32 function I can call to show a Windows login dialog?
Is there any example code of a cpython (not IronPython) client which can call
I know that one can call the following API to hibernate the system: SetSuspendState(TRUE,
When i call window.open, I can include a list of parameters. One of these

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.