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

  • SEARCH
  • Home
  • 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 7403571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:07:14+00:00 2026-05-29T05:07:14+00:00

How can I use SetProcessAffinityMask to select more than one logical processor? In Windows

  • 0

How can I use SetProcessAffinityMask to select more than one logical processor?

In Windows Task Manager you can do this as an example:

enter image description here

I updated my CreateProcess procedure to do this:

type
  TProcessPriority = (ptLow         = $00000040,
                      ptBelowNormal = $00004000,
                      ptNormal      = $00000020,
                      ptAboveNormal = $00008000,
                      ptHigh        = $00000080,
                      ptRealtime    = $00000100);

procedure RunProcess(FileName: string; Priority: TProcessPriority);
var
  StartInfo: TStartupInfo;
  ProcInfo: TProcessInformation;
  CmdLine: string;
  Done: Boolean;
begin
  FillChar(StartInfo, SizeOf(TStartupInfo), #0);
  FillChar(ProcInfo, SizeOf(TProcessInformation), #0);
  StartInfo.cb := SizeOf(TStartupInfo);

  CmdLine := FileName;
  UniqueString(CmdLine);
  try
    Done := CreateProcess(nil, PChar(CmdLine), nil, nil, False,
                          CREATE_NEW_PROCESS_GROUP + Integer(Priority),
                          nil, nil, StartInfo, ProcInfo);
    if Done then
    begin
      // Todo: Get actual cpu core count before attempting to set affinity!
      // 0 = <All Processors>
      // 1 = CPU 0
      // 2 = CPU 1
      // 3 = CPU 2
      // 4 = CPU 3
      // 5 = CPU 5
      // 6 = CPU 6
      // 7 = CPU 6
      // 8 = CPU 7

      // this sets to CPU 0 - but how to allow multiple parameters to
      // set more than one logical processor?
      SetProcessAffinityMask(ProcInfo.hProcess, 1); 
    end else
      MessageDlg('Could not run ' + FileName, mtError, [mbOk], 0)
  finally
    CloseHandle(ProcInfo.hProcess);
    CloseHandle(ProcInfo.hThread);
  end;
end;

Note the comments I put in there. It would be good to update my procedure to include a new Affinity parameter which I can pass to SetProcessAffinityMask.

Calling any of these won’t select the corresponding processors for obvious reasons, they give the idea of what I am wanting to do:

SetProcessAffinityMask(ProcInfo.hProcess, 1 + 2); 
SetProcessAffinityMask(ProcInfo.hProcess, 1 and 2);

eg, select any of the CPU’s for a process, as shown in Task Manager.

How should I do this, using an Array, Set or something else? I cannot get it to work with multiple values.

Thanks.

  • 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-29T05:07:15+00:00Added an answer on May 29, 2026 at 5:07 am

    It is a bitmask as described in the documentation.

    A process affinity mask is a bit vector in which each bit represents a logical processor on which the threads of the process are allowed to run.

    • Processor 0 is $01.
    • Processor 1 is $02.
    • Processor 2 is $04.
    • Processor 3 is $08.
    • Processor 4 is $10.

    And so on. You can use logical or to combine them. So processors 0 and 1 would be $01 or $02 which equals $03.

    I would use the shift operator shl to create values for specific processors. Like this:

    function SingleProcessorMask(const ProcessorIndex: Integer): DWORD_PTR;
    begin
      //When shifting constants the compiler will force the result to be 32-bit
      //if you have more than 32 processors, `Result:= 1 shl x` will return
      //an incorrect result.
      Result := DWORD_PTR(1) shl (ProcessorIndex); 
    end;
    

    You can readily extend this to generate masks for lists of processors using logical or in a loop.

    function CombinedProcessorMask(const Processors: array of Integer): DWORD_PTR;
    var
      i: Integer;
    begin
      Result := 0;
      for i := low(Processors) to high(Processors) do
        Result := Result or SingleProcessorMask(Processors[i]);
    end;
    

    You can test for a processor being in a bit mask like this:

    function ProcessorInMask(const ProcessorMask: DWORD_PTR; 
      const ProcessorIndex: Integer): Boolean;
    begin
      Result := (SingleProcessorMask(ProcessorIndex) and ProcessorMask)<>0;
    end;
    

    Note: I’m using DWORD_PTR because for 64 bit targets the bitmask is 64 bits wide. That nuance doesn’t matter for you on XE but it’s worth getting it right to make any future code porting easier.

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

Sidebar

Related Questions

You can use more than one css class in an HTML tag in current
I can use Resources res=this.getResources(); arr1=res.getStringArray(R.array.one); to get the array and changed some of
I can use Server[request_time] to get current timestamp. But how would I use this
I can use this, to find all instances of fly and replace it with
I can use this when I need multiple objects with different attributes: class struct(object):
I can use SELECT * FROM table WHERE id IN (478,278,190,890,123) to return a
How can use this text value? Text=My custom text: {Binding MyData} In this code?
Can one use mod_pagespeed with tomcat application server? directly or indirectly? Am i correct
We can use either the new condition variable primitive or windows event in order
We can use polling to find out about updates from some source, for example,

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.