home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
vp21beta.zip
/
LEXMPSRC.RAR
/
THREADS.PAS
< prev
next >
Wrap
Pascal/Delphi Source File
|
2000-08-15
|
3KB
|
111 lines
{█▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀█}
{█ █}
{█ Virtual Pascal for Linux █}
{█ Test example for thread handling █}
{█ ─────────────────────────────────────────────────█}
{█ Copyright (C) 1999 Joerg Pleumann █}
{█ █}
{▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀}
{$S+}
(**
* This program does the following:
*
* - It creates a secondary thread in addition to the
* main hread. The secondary thread is created in
* suspended state.
*
* - Both threads call a function ThreadFunc which
* basically counts to fifty.
*
* - The current counter value of each thread is stored
* in the global variable Value, which is a threadvar,
* so each thread has its own incarnation of the
* variable.
*
* - When thread 1 enconters a specific value, it does
* something with thread 2:
*
* - Value = 10: thread 2 is started.
* - Value = 20: thread 2 is suspended.
* - Value = 30: thread 2 is restarted again.
* - Value = 40: thread 2 is killed.
*)
program Threads;
uses
VpSysLow;
threadvar
Value: LongInt;
function ThreadFunc(Args: Pointer): LongInt;
var
Loop, Dummy: LongInt;
begin
WriteLn('Thread function invoked for thread ', GetThreadID, '.');
Value := 1;
while Value <= 50 do
begin
WriteLn('- Thread ', GetThreadID, ' has counted to ', Value, '.');
SysCtrlSleep(100);
if GetThreadID = 1 then
begin
if (Value = 10) then
begin
WriteLn('Thread 1 starts thread 2.');
ResumeThread(2);
end;
if (Value = 20) then
begin
WriteLn('Thread 1 suspends thread 2.');
SuspendThread(2);
end;
if (Value = 30) then
begin
WriteLn('Thread 1 restarts thread 2.');
ResumeThread(2);
end;
if (Value = 40) then
begin
WriteLn('Thread 1 kills thread 2.');
KillThread(2);
end;
end;
Inc(Value);
end;
end;
var
Err, Tid: LongInt;
begin
WriteLn;
WriteLn('<-- Thread handling / thread local storage demonstration program -->');
WriteLn('(See source code if you want to know what it really does.)');
Err := BeginThread(nil, 8192, ThreadFunc, nil, 1, Tid);
if Err < 0 then
begin
WriteLn('Error ', Err, ' creating secondary thread.');
Halt;
end;
WriteLn('Thread ', Tid, ' successfully created in suspended state.');
ThreadFunc(nil);
WriteLn('Program is finished.');
WriteLn;
end.