DelphiBasics
ThreadVar
Keyword
Defines variables that are given separate instances per thread
  ThreadVar
    Declarations;
Description
The ThreadVar keyword starts a set of variable definitions that are used by threads.
 
Each thread is given a separate instance of each variable, thereby avoiding data conflicts, and preserving thread independence.
 
Note that the example uses a pointer - the Delphi help indicates that this is unwise. Anyone who can provide a nice working replacement example that avoids using pointers will receive the Delphi Basics download program for free.
Notes
ThreadVars can only be freed automatically, if they have a fixed size. Thus strings, dynamic arrays, interfaces, etc. won’t be freed automatically.
Related commands
BeginThread Begins a separate thread of code execution
EndThread Terminates a thread with an exit code
IsMultiThread Returns true if the code is running multiple threads
 Author links

 Download this web site as a Windows program.





 
Example code : A simple example
// Full Unit code.
// -----------------------------------------------------------
// You must store this code in a unit called Unit1 with a form
// called Form1 that has an OnCreate event called FormCreate.

unit Unit1;

interface

uses
  Forms, Dialogs, Windows, SysUtils;

type
  TMsgRecord = record
    thread : Integer;
    msg    : string[30];
  end;
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  end;

var
  Form1: TForm1;

Implementation
{$R *.dfm}        // Include form definitions

ThreadVar         // We must allow each thread its own instances
                  // of the passed record variable
  msgPtr : ^TMsgRecord;

// Private thread procedure to show a string
function ShowMsg(Parameter : Pointer) : Integer;
begin
  // Set up a 0 return value
  Result := 0;

  // Map the pointer to the passed data
  // Note that each thread has a separate copy of msgPtr
  msgPtr := Parameter;

  // Display this message
  ShowMessagePos('Thread '+IntToStr(msgPtr.thread)+' '+msgPtr.msg,
                 200*msgPtr.thread, 100);

  // End the thread
  EndThread(0);
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  id1, id2 : LongWord;
  thread1, thread2 : Integer;
  msg1, msg2 : TMsgRecord;

begin
  // Set up our display messages
  msg1.thread := 1;
  msg1.msg    := 'Hello World';
  msg2.thread := 2;
  msg2.msg    := 'Goodbye World';

  // Start the first thread running asking for users first name
  thread1 := BeginThread(nil,
                         0,
                         Addr(ShowMsg),
                         Addr(msg1),
                         0,
                         id1);

  // And also ask for the surname
  thread2 := BeginThread(nil,
                         0,
                         Addr(ShowMsg),
                         Addr(msg2),
                         0,
                         id2);

  // Ensure that the threads are only closed when all done
  ShowMessagePos('Press this when other dialogs finished.', 200, 300);

  // Finally, tidy up by closing the threads
  CloseHandle(thread1);
  CloseHandle(thread2);
end;

end.
   Three dialogs are displayed:
     
   Thread 1 Hello World
   Thread 2 Goodbye World
   Press this when other dialogs finished.
 
Delphi Programming © Neil Moffatt 2002 - 2020. All rights reserved.  |  Home Page