home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 30 fixes_v / 30-fixes_v.zip / bp7-os2.zip / THREAD.PAS < prev    next >
Pascal/Delphi Source File  |  1993-10-22  |  1KB  |  57 lines

  1. Program ThreadTest;  {--- 1993 Matthias Withopf / c't ---}
  2.  
  3. Uses
  4.   Crt;
  5.  
  6.   Function DosCreateThread(Proc : Pointer;Var TID : Word;
  7.                Stack : Pointer) : Word; Far;
  8.     External 'DOSCALLS' Index 145;
  9.   Function DosWrite(Handle : Word;Str : PChar;Count : Word;
  10.             Var WCount : Word) : Word; Far;
  11.     External 'DOSCALLS' Index 138;
  12.   Function DosExit(x : Word;ExitCode : Word) : Word; Far;
  13.     External 'DOSCALLS' Index 5;
  14.  
  15. Const
  16.   Thread1Terminated : Boolean = False;
  17.  
  18. {$S-} { Stack checking off, this will have a seperate stack space. }
  19.  
  20.   Procedure Thread1; Far;
  21.   Const
  22.     Str = 'This is Thread1 active.'^M^J;
  23.   Var
  24.     w : Word;
  25.   Begin
  26.     Repeat
  27.       DosWrite(1,Str,Length(Str),w);
  28.       Delay(700);
  29.       If KeyPressed then
  30.     Begin
  31.       Thread1Terminated := True;
  32.       DosExit(0,0);  { Terminate only Thread1, not the program. }
  33.     End;
  34.     Until False;
  35.   End;
  36.  
  37. {$S+} { Stack Checking on, this will be in the main stack. }
  38.  
  39. Const
  40.   StackSize = 8192;
  41. Var
  42.   Thread1ID : Word;
  43.   Stack1    : Pointer;
  44. Begin
  45.   GetMem(Stack1,StackSize);         { Allocate a stack for Thread1. }
  46.   DosCreateThread(@Thread1,         { Address of procedure Thread1 }
  47.           Thread1ID,
  48.           { And the stack pointer to initialize SS:SP. }
  49.           @PChar(Stack1)[StackSize]);
  50.   Repeat
  51.     WriteLn('The main program unit is active.');
  52.     Delay(1100);
  53.   Until Thread1Terminated;
  54.   FreeMem(Stack1,StackSize);         { Free memory for Thread1's stack. }
  55.   WriteLn('Program ends');
  56. End.
  57.