📄 threadunit.pas
字号:
unit threadUnit;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Gauges;
type
TForm1 = class(TForm)
CreateThread: TButton;
procedure CreateThreadClick(Sender: TObject);
private
ThreadHandle : THandle;
ThreadHandle1 : THandle;
ThreadHandle2 : THandle;
MutexHandle: THandle;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
function ThreadFunc0(Info: Pointer): Integer; stdcall
var
ICount: Integer; // general loop counter
CountStr: string; // holds a string representation of the counter
begin
{wait for the mutex to become signaled. the mutex is created signaled so
this thread gets ownership of the mutex and starts immediatly}
WaitForSingleObject(Form1.MutexHandle, INFINITE);
{start a counter to display something}
for ICount := 1 to 10000 do
begin
CountStr := IntToStr(ICount);
Form1.Canvas.TextOut(10, 10, 'Thread 1 '+CountStr);
end;
{Release ownership of the mutex so the other threads can fire}
ReleaseMutex(Form1.MutexHandle);
ExitThread(1);
end;
function ThreadFunc1(Info: Pointer): Integer; stdcall
var
ICount: Integer; // general loop counter
CountStr: string; // holds a string representation of the counter
begin
{wait for the mutex to become signaled. the mutex is created signaled so
this thread gets ownership of the mutex and starts immediatly}
WaitForSingleObject(Form1.MutexHandle, INFINITE);
{start a counter to display something}
for ICount := 1 to 10000 do
begin
CountStr := IntToStr(ICount);
Form1.Canvas.TextOut(110, 10, 'Thread 2 '+CountStr);
end;
{Release ownership of the mutex so the other threads can fire}
ReleaseMutex(Form1.MutexHandle);
ExitThread(2);
end;
function ThreadFunc2(Info: Pointer): Integer; stdcall
var
ICount: Integer; // general loop counter
CountStr: string; // holds a string representation of the counter
LocalMutexHandle: THandle; // holds a handle to the mutex
begin
{open a Handle to the mutex from this thread}
LocalMutexHandle := OpenMutex(MUTEX_ALL_ACCESS, FALSE, 'MyMutex');
{take ownership of the mutex. this will wait until the mutex is signaled}
WaitForSingleObject(LocalMutexHandle, INFINITE);
{start a counter to display something}
for ICount := 1 to 10000 do
begin
CountStr := IntToStr(ICount);
Form1.canvas.TextOut(210, 10, 'Thread 3 '+CountStr);
end;
{Release ownership of the mutex}
ReleaseMutex(LocalMutexHandle);
{close the mutex handle}
CloseHandle(LocalMutexHandle);
ExitThread(3);
end;
procedure TForm1.CreateThreadClick(Sender: TObject);
var
ThreadId0, ThreadId1, ThreadId2: DWORD; // holds thread identifiers
begin
{Create the mutex with the name MyMutex. the mutex is signaled
so the first thread will start imediatly}
MutexHandle := CreateMutex(nil, False, 'MyMutex');
{Create the first thread, and start it immediatly}
ThreadHandle := Windows.CreateThread(nil, 0, @ThreadFunc0, nil, 0, ThreadId0);
{Create the second thread, and start it immediatly}
ThreadHandle1 := Windows.CreateThread(nil,0, @ThreadFunc1, nil, 0, ThreadId1);
{Create the third thread, and start it immediatly}
ThreadHandle2 := Windows.CreateThread(nil,0, @ThreadFunc2, nil, 0, ThreadId2);
{Stop the main thread for a short time so that the other threads get
a chance to take ownership of the mutex before the main thread
calls WaitForSingleObject}
Sleep(1000);
{Take ownership of the mutex; this will wait until the mutex is signaled}
WaitForSingleObject(MutexHandle, INFINITE);
{Close the mutexHandle so that this will work agian}
CloseHandle(MutexHandle);
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -