MultithreadingAndCriticalsections/fr: what changed
Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | 1 | ## Démonstration de programmation de Thread | |
| 2 | 2 | ||
| 3 | 3 | ``` pascal | |
| 4 | 4 | {$mode objfpc}{$H+} | |
| 5 | 5 | program Project1; | |
| 6 | 6 | ||
| 7 | 7 | uses | |
| 8 | 8 | {$ifdef unix}cthreads, | |
| 9 | 9 | {$endif}Classes, | |
| 10 | 10 | SysUtils; | |
| 11 | 11 | ||
| 12 | 12 | var | |
| 13 | 13 | CriticalSection: TRTLCriticalSection; | |
| 14 | 14 | Counter: integer; | |
| 15 | 15 | UseCriticalSection: boolean; | |
| 16 | 16 | ||
| 17 | 17 | function Supposedly: string; | |
| 18 | 18 | begin | |
| 19 | 19 | if UseCriticalSection then | |
| 20 | 20 | Result:='' | |
| 21 | 21 | else Result:='supposedly '; | |
| 22 | 22 | end; | |
| 23 | 23 | ||
| 24 | 24 | type | |
| 25 | 25 | { TMyThread } | |
| 26 | 26 | ||
| 27 | 27 | TMyThread = class(TThread) | |
| 28 | 28 | private | |
| 29 | 29 | FAFinished: boolean; | |
| 30 | 30 | FID: integer; | |
| 31 | 31 | public | |
| 32 | 32 | constructor Create(CreateSuspended: Boolean; ID: integer; const StackSize: SizeUInt=DefaultStackSize); | |
| 33 | 33 | procedure Execute; override; | |
| 34 | 34 | property AFinished: boolean read FAFinished write FAFinished; | |
| 35 | 35 | end; | |
| 36 | 36 | ||
| 37 | 37 | constructor TMyThread.Create(CreateSuspended: Boolean; ID: integer; const StackSize: SizeUInt); | |
| 38 | 38 | begin | |
| 39 | 39 | inherited Create(CreateSuspended, StackSize); | |
| 40 | 40 | FID:=ID; | |
| 41 | 41 | end; | |
| 42 | 42 | ||
| 43 | 43 | procedure TMyThread.Execute; | |
| 44 | 44 | var | |
| 45 | 45 | j: integer; | |
| 46 | 46 | begin | |
| 47 | 47 | FAFinished := False; | |
| 48 | 48 | if UseCriticalSection then | |
| 49 | 49 | EnterCriticalSection(CriticalSection); | |
| 50 | 50 | try | |
| 51 | 51 | for j := 1 to 1000000 do | |
| 52 | 52 | Inc(Counter); | |
| 53 | 53 | WriteLn(Format('Thread %d has %sfinished making 1000000 increments to Counter',[FID, Supposedly])); | |
| 54 | 54 | finally | |
| 55 | 55 | if UseCriticalSection then | |
| 56 | 56 | LeaveCriticalSection(CriticalSection); | |
| 57 | 57 | end; | |
| 58 | 58 | FAFinished := True; | |
| 59 | 59 | end; | |
| 60 | 60 | ||
| 61 | 61 | function HasCriticalSection: string; | |
| 62 | 62 | begin | |
| 63 | 63 | if UseCriticalSection then | |
| 64 | 64 | Result:='' | |
| 65 | 65 | else Result:='NOT '; | |
| 66 | 66 | end; | |
| 67 | 67 | ||
| 68 | 68 | procedure ExerciseThreads(aUseCriticalSection: boolean); | |
| 69 | 69 | var | |
| 70 | 70 | Threads: array[1..20] of TMyThread; | |
| 71 | 71 | AllFinished: boolean; | |
| 72 | 72 | i: integer; | |
| 73 | 73 | begin | |
| 74 | 74 | Counter := 0; | |
| 75 | 75 | UseCriticalSection:=aUseCriticalSection; | |
| 76 | 76 | WriteLn(Format('A critical section is %sbeing used; Counter starts at %d',[HasCriticalSection, Counter])); | |
| 77 | 77 | // start 20 threads | |
| 78 | 78 | for i := Low(Threads) to High(Threads) do | |
| 79 | 79 | Threads[i] := TMyThread.Create(False, i); | |
| 80 | 80 | // wait till all threads finished | |
| 81 | 81 | repeat | |
| 82 | 82 | AllFinished := True; | |
| 83 | 83 | for i := Low(Threads) to High(Threads) do | |
| 84 | 84 | if not Threads[i].AFinished then | |
| 85 | 85 | AllFinished := False; | |
| 86 | 86 | until AllFinished; | |
| 87 | 87 | // free the threads | |
| 88 | 88 | for i := Low(Threads) to High(Threads) do | |
| 89 | 89 | Threads[i].Free; | |
| 90 | 90 | WriteLn('Final value of Counter=' + IntToStr(Counter)); | |
| 91 | 91 | WriteLn; | |
| 92 | 92 | end; | |
| 93 | 93 | ||
| 94 | 94 | var | |
| 95 | 95 | b: boolean; | |
| 96 | 96 | begin | |
| 97 | 97 | // create the CriticalSection | |
| 98 | 98 | InitCriticalSection(CriticalSection); | |
| 99 | 99 | for b in Boolean do | |
| 100 | 100 | ExerciseThreads(b); | |
| 101 | 101 | // free the CriticalSection | |
| 102 | 102 | DoneCriticalSection(CriticalSection); | |
| 103 | 103 | ||
| 104 | 104 | {$IfDef WINDOWS} | |
| 105 | 105 | Write(' Press [Enter] to finish'); | |
| 106 | 106 | ReadLn; | |
| 107 | 107 | {$EndIf} | |
| 108 | 108 | end. | |
| 109 | 109 | ``` | |
| 110 | 110 | ||
| 111 | - | ||
| 112 | - | ||
| 113 | 111 | --- | |
| 114 | 112 | *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).* |
