Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
95
Task/Mutex/Delphi/mutex.delphi
Normal file
95
Task/Mutex/Delphi/mutex.delphi
Normal file
|
|
@ -0,0 +1,95 @@
|
|||
unit main;
|
||||
|
||||
interface
|
||||
|
||||
uses
|
||||
Winapi.Windows, System.SysUtils, System.Classes, Vcl.Controls, Vcl.Forms,
|
||||
System.SyncObjs, Vcl.StdCtrls;
|
||||
|
||||
type
|
||||
TForm1 = class(TForm)
|
||||
mmo1: TMemo;
|
||||
btn1: TButton;
|
||||
procedure FormCreate(Sender: TObject);
|
||||
procedure FormDestroy(Sender: TObject);
|
||||
procedure btn1Click(Sender: TObject);
|
||||
private
|
||||
{ Private declarations }
|
||||
public
|
||||
{ Public declarations }
|
||||
end;
|
||||
|
||||
var
|
||||
Form1: TForm1;
|
||||
FMutex: TMutex;
|
||||
|
||||
implementation
|
||||
|
||||
{$R *.dfm}
|
||||
|
||||
procedure TForm1.FormCreate(Sender: TObject);
|
||||
begin
|
||||
FMutex := TMutex.Create();
|
||||
end;
|
||||
|
||||
procedure TForm1.FormDestroy(Sender: TObject);
|
||||
begin
|
||||
FMutex.Free;
|
||||
end;
|
||||
|
||||
// http://edgarpavao.com/2017/08/07/multithreading-e-processamento-paralelo-no-delphi-ppl/
|
||||
procedure TForm1.btn1Click(Sender: TObject);
|
||||
begin
|
||||
//Thread 1
|
||||
TThread.CreateAnonymousThread(
|
||||
procedure
|
||||
begin
|
||||
FMutex.Acquire;
|
||||
try
|
||||
TThread.Sleep(5000);
|
||||
TThread.Synchronize(TThread.CurrentThread,
|
||||
procedure
|
||||
begin
|
||||
mmo1.Lines.Add('Thread 1');
|
||||
end);
|
||||
finally
|
||||
FMutex.Release;
|
||||
end;
|
||||
end).Start;
|
||||
|
||||
//Thread 2
|
||||
TThread.CreateAnonymousThread(
|
||||
procedure
|
||||
begin
|
||||
FMutex.Acquire;
|
||||
try
|
||||
TThread.Sleep(1000);
|
||||
TThread.Synchronize(TThread.CurrentThread,
|
||||
procedure
|
||||
begin
|
||||
mmo1.Lines.Add('Thread 2');
|
||||
end);
|
||||
finally
|
||||
FMutex.Release;
|
||||
end;
|
||||
end).Start;
|
||||
|
||||
//Thread 3
|
||||
TThread.CreateAnonymousThread(
|
||||
procedure
|
||||
begin
|
||||
FMutex.Acquire;
|
||||
try
|
||||
TThread.Sleep(3000);
|
||||
TThread.Synchronize(TThread.CurrentThread,
|
||||
procedure
|
||||
begin
|
||||
mmo1.Lines.Add('Thread 3');
|
||||
end);
|
||||
finally
|
||||
FMutex.Release;
|
||||
end;
|
||||
end).Start;
|
||||
end;
|
||||
|
||||
end.
|
||||
Loading…
Add table
Add a link
Reference in a new issue