AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Programmierung allgemein Win32/Win64 API (native code) Delphi BeginThreadAffinity() or SetThreadAffinity
Thema durchsuchen
Ansicht
Themen-Optionen

BeginThreadAffinity() or SetThreadAffinity

Ein Thema von Razor · begonnen am 7. Mai 2008 · letzter Beitrag vom 9. Mai 2008
Antwort Antwort
Razor
(Gast)

n/a Beiträge
 
#1

BeginThreadAffinity() or SetThreadAffinity

  Alt 7. Mai 2008, 17:40
Well how to make it like that the functionons or procedures run on multiple cores,example 1 function to be run on all 4 cores of a cpu.And example if anybody knows how to do it,yes ive checked the msdn searched google no luck...
  Mit Zitat antworten Zitat
Muetze1
(Gast)

n/a Beiträge
 
#2

Re: BeginThreadAffinity() or SetThreadAffinity

  Alt 7. Mai 2008, 17:42
1. It is default for every process/thread to run on all cores/CPUs of the system.
2. MSDN-Library durchsuchenSetThreadAffinityMask() & MSDN-Library durchsuchenSetProcessAffinityMask() need a mask on call, so you define the usable cores/CPUs
  Mit Zitat antworten Zitat
grenzgaenger
(Gast)

n/a Beiträge
 
#3

Re: BeginThreadAffinity() or SetThreadAffinity

  Alt 7. Mai 2008, 17:42
you can use different threads.

in general you use only one thread, and so your program work sequential.

kind regards
GG
  Mit Zitat antworten Zitat
Razor
(Gast)

n/a Beiträge
 
#4

Re: BeginThreadAffinity() or SetThreadAffinity

  Alt 7. Mai 2008, 17:44
Yea but how to make threads ,becouse of this code
I figured myself..and intel manuals..

Delphi-Quellcode:
procedure TForm1.FormCreate(Sender: TObject);
VAR
EAX,EDX:DWORD;
Temperature ,delta,maxcores,x,TJunction:integer;

begin
InitializeDll();
cxCpu401.Available.Available:=maxcores;









Rdmsr($00EE,EAX,EDX);
IF EAX and $40000000 = 0 then TJunction := 100 Else TJunction := 85;
form1.Caption:=inttostr(TJunction);


For X := 1 to MaxCores do
Begin
Delta := TJunction;
SetThreadAffinty(X) ;// makes that the function now runs on core X //this<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<,
Rdmsr($019,EAX,EDX);
If HiWord(EAX) and $8000 > 0 then
Delta := HiWord(EAX) and $7F; // Delta-value from the temperature for reading TJunction
Temperature := TJunction-Delta;
End;
  Mit Zitat antworten Zitat
grenzgaenger
(Gast)

n/a Beiträge
 
#5

Re: BeginThreadAffinity() or SetThreadAffinity

  Alt 7. Mai 2008, 17:47
Zitat von Razor:
Delphi-Quellcode:
For X := 1 to MaxCores do
Begin
Delta := TJunction;
SetThreadAffinty(X) ;// makes that the function now runs on core X //this<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<,
Rdmsr($019,EAX,EDX);
If HiWord(EAX) and $8000 > 0 then
Delta := HiWord(EAX) and $7F; // Delta-value from the temperature for reading TJunction
Temperature := TJunction-Delta;
End;
you can use descantands of the class tThread (take a look to your delphi help). but don't use things like your code above. the distribution of the threads to the differend cores and cpu's is the task of your operating system.
  Mit Zitat antworten Zitat
Muetze1
(Gast)

n/a Beiträge
 
#6

Re: BeginThreadAffinity() or SetThreadAffinity

  Alt 7. Mai 2008, 17:49
He tries to get the core temperatures for each core, so he needs this assignment...
  Mit Zitat antworten Zitat
Razor
(Gast)

n/a Beiträge
 
#7

Re: BeginThreadAffinity() or SetThreadAffinity

  Alt 7. Mai 2008, 17:49
so you think it should work without that thread distrubution setting wich is crazy imho
  Mit Zitat antworten Zitat
grenzgaenger
(Gast)

n/a Beiträge
 
#8

Re: BeginThreadAffinity() or SetThreadAffinity

  Alt 7. Mai 2008, 17:52
Zitat von Muetze1:
He tries to get the core temperatures for each core, so he needs this assignment...
mhh, in this case, he has to distribute his threads manually.

kind regards
GG
  Mit Zitat antworten Zitat
Razor
(Gast)

n/a Beiträge
 
#9

Re: BeginThreadAffinity() or SetThreadAffinity

  Alt 7. Mai 2008, 18:01
Threads is this cool to use?

Delphi-Quellcode:
unit PrimeForm;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TPrimeFrm = class(TForm)
    NumEdit: TEdit;
    SpawnButton: TButton;
    ResultsMemo: TMemo;
    procedure SpawnButtonClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  private
    { Private declarations }
    FThreadRefCount: integer;
    procedure HandleTerminate(Sender: TObject);
  public
    { Public declarations }
  end;

var
  PrimeFrm: TPrimeFrm;

implementation

uses PrimeThread;

{$R *.DFM}

procedure TPrimeFrm.SpawnButtonClick(Sender: TObject);

var
  NewThread: TPrimeThrd;

begin
  NewThread := TPrimeThrd.Create(True);
  NewThread.FreeOnTerminate := True;
  try
    with NewThread do
    begin
      TestNumber := StrToInt(NumEdit.Text);
      Inc(FThreadRefCount);
      OnTerminate := HandleTerminate;
      Resume;
    end;
  except on EConvertError do
    begin
      NewThread.Free;
      ShowMessage('That is not a valid number!');
    end;
  end;
end;

procedure TPrimeFrm.FormCreate(Sender: TObject);
begin
  FThreadRefCount := 0;
end;

procedure TPrimeFrm.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  CanClose := true;
  if FThreadRefCount > 0 then
  begin
    if MessageDlg('Threads active. Do you still want to quit?',
      mtWarning, [mbYes, mbNo], 0) = mrNo then
      CanClose := false;
  end;
end;

procedure TPrimeFrm.HandleTerminate(Sender: TObject);
begin
  Dec(FThreadRefCount);
end;

end.
  Mit Zitat antworten Zitat
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
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 12:41 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz