Sign in Join
Wiki copytemporary

MultithreadingAndCriticalsections/fr: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11## Démonstration de programmation de Thread
22
33``` pascal
44{$mode objfpc}{$H+}
55program Project1;
66
77uses
88 {$ifdef unix}cthreads,
99 {$endif}Classes,
1010 SysUtils;
1111
1212var
1313 CriticalSection: TRTLCriticalSection;
1414 Counter: integer;
1515 UseCriticalSection: boolean;
1616
1717 function Supposedly: string;
1818 begin
1919 if UseCriticalSection then
2020 Result:=''
2121 else Result:='supposedly ';
2222 end;
2323
2424type
2525 { TMyThread }
2626
2727 TMyThread = class(TThread)
2828 private
2929 FAFinished: boolean;
3030 FID: integer;
3131 public
3232 constructor Create(CreateSuspended: Boolean; ID: integer; const StackSize: SizeUInt=DefaultStackSize);
3333 procedure Execute; override;
3434 property AFinished: boolean read FAFinished write FAFinished;
3535 end;
3636
3737 constructor TMyThread.Create(CreateSuspended: Boolean; ID: integer; const StackSize: SizeUInt);
3838 begin
3939 inherited Create(CreateSuspended, StackSize);
4040 FID:=ID;
4141 end;
4242
4343 procedure TMyThread.Execute;
4444 var
4545 j: integer;
4646 begin
4747 FAFinished := False;
4848 if UseCriticalSection then
4949 EnterCriticalSection(CriticalSection);
5050 try
5151 for j := 1 to 1000000 do
5252 Inc(Counter);
5353 WriteLn(Format('Thread %d has %sfinished making 1000000 increments to Counter',[FID, Supposedly]));
5454 finally
5555 if UseCriticalSection then
5656 LeaveCriticalSection(CriticalSection);
5757 end;
5858 FAFinished := True;
5959 end;
6060
6161 function HasCriticalSection: string;
6262 begin
6363 if UseCriticalSection then
6464 Result:=''
6565 else Result:='NOT ';
6666 end;
6767
6868 procedure ExerciseThreads(aUseCriticalSection: boolean);
6969 var
7070 Threads: array[1..20] of TMyThread;
7171 AllFinished: boolean;
7272 i: integer;
7373 begin
7474 Counter := 0;
7575 UseCriticalSection:=aUseCriticalSection;
7676 WriteLn(Format('A critical section is %sbeing used; Counter starts at %d',[HasCriticalSection, Counter]));
7777 // start 20 threads
7878 for i := Low(Threads) to High(Threads) do
7979 Threads[i] := TMyThread.Create(False, i);
8080 // wait till all threads finished
8181 repeat
8282 AllFinished := True;
8383 for i := Low(Threads) to High(Threads) do
8484 if not Threads[i].AFinished then
8585 AllFinished := False;
8686 until AllFinished;
8787 // free the threads
8888 for i := Low(Threads) to High(Threads) do
8989 Threads[i].Free;
9090 WriteLn('Final value of Counter=' + IntToStr(Counter));
9191 WriteLn;
9292 end;
9393
9494var
9595 b: boolean;
9696begin
9797 // create the CriticalSection
9898 InitCriticalSection(CriticalSection);
9999 for b in Boolean do
100100 ExerciseThreads(b);
101101 // free the CriticalSection
102102 DoneCriticalSection(CriticalSection);
103103
104104 {$IfDef WINDOWS}
105105 Write(' Press [Enter] to finish');
106106 ReadLn;
107107 {$EndIf}
108108end.
109109```
110110
111-\
112-
113111---
114112*From the Free Pascal wiki, page "MultithreadingAndCriticalsections/fr", as it stood on 2020-02-20 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history