Changeset 157 for MicroThreading/UMicroThreading.pas
- Timestamp:
- Jan 28, 2011, 7:33:14 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
MicroThreading/UMicroThreading.pas
r156 r157 119 119 end; 120 120 121 TMicroThreadThreadState = (ttsReady, ttsRunning, ttsTerminated); 122 121 123 { TMicroThreadThread } 122 124 123 125 TMicroThreadThread = class(TThread) 124 126 Manager: TMicroThreadManager; 125 ExecuteTerminated: Boolean;127 State: TMicroThreadThreadState; 126 128 procedure Execute; override; 127 129 constructor Create(CreateSuspended: Boolean; … … 140 142 FExecuteCount: Integer; 141 143 FExecutedCount: Integer; 142 FTerminated: Boolean;143 144 FCurrentMicroThread: TMicroThread; 144 145 FScheduler: TMicroThreadScheduler; … … 187 188 procedure Start; 188 189 procedure Stop; 189 procedure PoolThreadTerminated(Sender: TObject);190 190 procedure UpdateThreadPoolSize; 191 191 procedure MainThreadStart(Sender: TObject); … … 197 197 destructor Destroy; override; 198 198 property ThreadPool: TObjectList read FThreadPool; 199 property ThreadPoolLock: TCriticalSection read FThreadPoolLock; 199 200 property ThreadPoolSize: Integer read GetThreadPoolSize 200 201 write SetThreadPoolSize; … … 216 217 MicroThreadBlockStateText: array[TMicroThreadBlockState] of string = ('None', 217 218 'Sleeping', 'WaitFor', 'Terminating', 'Terminated'); 219 MicroThreadThreadStateText: array[TMicroThreadThreadState] of string = ( 220 'Ready', 'Running', 'Terminated'); 218 221 219 222 function GetCurrentMicroThread: TMicroThread; … … 503 506 try 504 507 repeat 505 ExecutedCount := Manager.Execute(100000); 508 State := ttsRunning; 509 ExecutedCount := Manager.Execute(10); 510 State := ttsReady; 506 511 if ExecutedCount = 0 then Sleep(1); 507 512 until Terminated; … … 516 521 begin 517 522 inherited; 518 ExecuteTerminated := False;523 State := ttsReady; 519 524 Manager := TMicroThreadManager.Create; 520 525 end; … … 522 527 destructor TMicroThreadThread.Destroy; 523 528 begin 524 Terminate;525 repeat526 Sleep(1);527 until ExecuteTerminated;528 529 529 Manager.Free; 530 530 inherited Destroy; … … 758 758 begin 759 759 FState := ssTerminating; 760 // Wait for all thread managers to finish 760 761 try 761 762 FThreadPoolLock.Acquire; … … 763 764 TMicroThreadThread(FThreadPool[I]).Terminate; 764 765 end; 766 for I := 0 to FThreadPool.Count - 1 do begin 767 TMicroThreadThread(FThreadPool[I]).WaitFor; 768 end; 769 FThreadPool.Clear; 765 770 finally 766 771 FThreadPoolLock.Release; 767 772 end; 768 773 769 // Wait for all thread managers to finish770 774 repeat 771 775 Application.ProcessMessages; 772 776 Sleep(1); 773 until FMainThreadTerminated and (ThreadPoolCount = 0);777 until FMainThreadTerminated or (not FUseMainThread); 774 778 FState := ssStopped; 775 end;776 777 procedure TMicroThreadScheduler.PoolThreadTerminated(Sender: TObject);778 var779 ThreadIndex: Integer;780 begin781 TMicroThreadThread(Sender).ExecuteTerminated := True;782 try783 FThreadPoolLock.Acquire;784 FThreadPool.OwnsObjects := False;785 ThreadIndex := FThreadPool.IndexOf(Sender);786 if ThreadIndex = -1 then787 raise Exception.Create('Trying to free thread not found in thread pool');788 FThreadPool.Delete(ThreadIndex);789 finally790 FThreadPool.OwnsObjects := True;791 FThreadPoolLock.Release;792 end;793 779 end; 794 780 … … 806 792 NewThread.Manager.FId := FThreadPool.Count + 1; 807 793 NewThread.Manager.FThread := NewThread; 808 NewThread.OnTerminate := PoolThreadTerminated;809 NewThread.FreeOnTerminate := True;794 //NewThread.OnTerminate := PoolThreadTerminated; 795 NewThread.FreeOnTerminate := False; 810 796 ThreadPool.Add(NewThread); 811 797 NewThread.Resume; … … 813 799 end else begin 814 800 while FThreadPool.Count > FThreadPoolSize do begin 801 TMicroThreadThread(FThreadPool[FThreadPool.Count - 1]).Terminate; 802 TMicroThreadThread(FThreadPool[FThreadPool.Count - 1]).WaitFor; 815 803 FThreadPool.Delete(FThreadPool.Count - 1); 816 804 end; … … 946 934 947 935 procedure TMicroThreadScheduler.SetThreadPoolSize(const AValue: Integer); 948 var949 I: Integer;950 NewThread: TMicroThreadThread;951 936 begin 952 937 FThreadPoolSize := AValue;
Note:
See TracChangeset
for help on using the changeset viewer.