Einzelnen Beitrag anzeigen

Razor
(Gast)

n/a Beiträge
 
#10

Re: BeginThreadAffinity() or SetThreadAffinity

  Alt 9. Mai 2008, 12:53
Here we go this is more like it 8)

AffinityMask CPU to use
1 0
2 1
3 0,1
4 2
5 0,2
6 0,1,2

function SetThreadAffinityMask(hThread: THandle; dwThreadAffinityMask: DWORD): DWORD; stdcall;


Steps:
1. Create a new class derived from TThread.
2. Include an AffinityMask property (or type DWORD) with a SetAffinityMask procedure.
3. Call SetThreadAffinityMask on the SetAffinityMask procedure.
4. Change your programs to inherit from your new thread class.
5. Use your new property MyThread.AffinityMask = 3 (3 for dual processor systems)

Delphi-Quellcode:
Code


unit ExThread;
 
interface
 
uses
  Classes;
 
type
  TExThread = class(TThread)
  private
    FAffinityMask: DWord;
    procedure SetAffinity(const Value: DWord);
    { Private declarations } 
  protected
    procedure Execute; override;
  public
    property AffinityMask : DWord read FAffinityMask write SetAffinity;
  end;
 
implementation
 
{ Important: Methods and properties of objects in VCL can only be used in a
  method called using Synchronize, for example,

      Synchronize(UpdateCaption);

  and UpdateCaption could look like,

    procedure TExThread.UpdateCaption;
    begin
      Form1.Caption := 'Updated in a thread';
    end; }
 
 
{ TExThread } 
 
procedure TExThread.Execute;
begin
  { Place thread code here } 
end;
 
procedure TExThread.SetAffinity(const Value: DWord);
begin
  FAffinityMask := SetThreadAffinityMask(Handle,Value);
  if FAffinityMask = 0 then raise Exception.Create('Error setting thread affinity mask : ' + IntToStr(GetLastError));
end;
 
end.
  Mit Zitat antworten Zitat