RSS订阅 | 匿名投稿
您的位置:网站首页 > 进程 > 正文

delphi 多核机器上编程实现将指定进程PID放到指定CPU上运行

作者:admin 来源: 日期:2018/2/11 20:25:43 人气: 标签:

BOOL SetProcessAffinityMask( 

  HANDLE hProcess,                 // handle to process 

  DWORD_PTR dwProcessAffinityMask  // process affinity mask 

); 

 

procedure SetCPU(h: THandle; CpuNo: Integer);  //cpuNo = 1..n 

var 

  ProcessAffinity: Cardinal; 

  _SystemAffinity: Cardinal; 

begin 

  GetProcessAffinityMask(h, ProcessAffinity, _SystemAffinity); 

  ProcessAffinity := CpuNo; 

  SetProcessAffinityMask(h, ProcessAffinity) 

end; 

 

setCpu(GetCurrentProcess, 2); //到 cpu 核心 2 上面运行

 

 

呼~我試出來了!!

原來Mutli-thread是可以的!

但是若是用到Synchronize,則會是只有main thread在執行..

所以即使有再多的thread..

也是只有一個thread在執行


參考來源:http://delphi.ktop.com.tw/board.php?cid=168&fid=912&tid=62520



type

  TCPUTester = Class(TThread)
    private
        FButton: TSpeedButton;
    protected
        procedure Execute;override;
    public
        constructor Create(Button:TSpeedButton ;
                           CreateSuspended: Boolean=true;
                           CPUMark: LongWord=1);
        destructor Destroy;override;

  end;

 

procedure TCPUTester.Execute;

begin
    while ((Not Terminated) and (FButton.Down)) do;
end;
  
constructor TCPUTester.Create(Button: TSpeedButton ;
                            CreateSuspended: boolean;
                            CPUMark: LongWord)
  
begin
    FButton := Button;
    Inherited Create(CreateSuspended);
    FreeOnTerminate:=true;
    SetThreadAffinityMask (Handle,1 shr (CPUMark-1));
end;
destructor TCPUTester.Destroy;
begin
    FButton:=NIL;
    Inherited;
end;
  
//---------------------------------------------------------------------------
procedure TForm1.FormCreate(Sender: TObject);
var
    ProcessAffinityMask: LongWord;
    SystemAffinityMask: LongWord;
    I: Integer;
begin
    if Not(GetProcessAffinityMask(    GetCurrentProcess(),
                                ProcessAffinityMask,
                                SystemAffinityMask)) then begin
        Application.Terminate();
        exit;
    end;
  
    for  i:=0 to sizeof(SystemAffinityMask)*8 - 1 do begin
        if(SystemAffinityMask and (1 shr i)) <> 0 then
        begin
            TSpeedButton(FindComponent('SpeedButton'+IntToStr(i+1))).Enabled:=true;
        end;
    end;
  
end;
//---------------------------------------------------------------------------
procedure TForm1.SpeedButton1Click(Sender: TObject );
var
  CPUTester:TCPUTester ;
  Button: TSpeedButton ;
begin
    if Not (Sender Is TSpeedButton) then
    begin
        Exit;
    end;
    Button:=Sender as TSpeedButton;
    if(NIL=Button)then Exit;
  
    if Not(Button.Down) then
    begin
        Exit;
    end;
    CPUTester:= TCPUTester.Create(Button,true,StrToInt(Button.Caption));
    if(NIL=CPUTester) then
    begin
        exit;
    end;
    CPUTester.Resume();
end;
//---------------------------------------------------------------------------
procedure TForm1.FormDestroy(Sender: TObject );
var
  I: Integer;
  comp: TComponent;
begin
   for i:=0 to ComponentCount-1 do
   begin
       comp:=Components[i];
       if(comp is TSpeedButton) then
         TSpeedButton(comp).Down:=false;
   end;
   Sleep(1000);
end;

读完这篇文章后,您心情如何?
0
0
0
0
0
0
0
0
本文网址:
下一篇:没有资料