OOP Delphi

Title: Extending TThread for multiple processor environments.
Question: In a search for a great performance in our programs, we try everything, from reduce the memory consumption to the ugly assembly code. But sometimes is easier to use some hardware resource available... in this case I will adjust my program to use a multiple processors if they are available.
Answer:
On windows environment multiprocessor capability is available only on Windows NT and Windows 2000, therefore our code is directed to those OS's.
How does windows manage the multiple processors?
In basic scene Windows switch process from one processor to another depending of the availability of each processor, meaning that your program is never using more than one processor at a time, but running on the less-loaded processor.
How to use more than one processor at the same time?
Threads are an easy way to use multiple processors at a time, allowing us to keep our process on one processor and distributing threads for others processors. Calling SetThreadAffinityMask from the Windows API to extend the TThread class is easy to modify our programs to get better performance from the hardware resources.
How does SetThreadAffinityMask works?
SetThreadAffinityMask expect 2 parameters thread handle (Handle property from TThread) and dwThreadAffinityMask. The dThreadAffinityMask parameter is a DWORD value on where each bit represent a processor starting on bit one for CPU 0, for that matter
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)
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.