Ignore:
Timestamp:
Jan 28, 2011, 7:33:14 PM (14 years ago)
Author:
george
Message:
  • Added: Displaying thread list in Demo.
  • Fixed: Terminating and freeing unused threads.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • MicroThreading/UMicroThreading.pas

    r156 r157  
    119119  end;
    120120
     121  TMicroThreadThreadState = (ttsReady, ttsRunning, ttsTerminated);
     122
    121123  { TMicroThreadThread }
    122124
    123125  TMicroThreadThread = class(TThread)
    124126    Manager: TMicroThreadManager;
    125     ExecuteTerminated: Boolean;
     127    State: TMicroThreadThreadState;
    126128    procedure Execute; override;
    127129    constructor Create(CreateSuspended: Boolean;
     
    140142    FExecuteCount: Integer;
    141143    FExecutedCount: Integer;
    142     FTerminated: Boolean;
    143144    FCurrentMicroThread: TMicroThread;
    144145    FScheduler: TMicroThreadScheduler;
     
    187188    procedure Start;
    188189    procedure Stop;
    189     procedure PoolThreadTerminated(Sender: TObject);
    190190    procedure UpdateThreadPoolSize;
    191191    procedure MainThreadStart(Sender: TObject);
     
    197197    destructor Destroy; override;
    198198    property ThreadPool: TObjectList read FThreadPool;
     199    property ThreadPoolLock: TCriticalSection read FThreadPoolLock;
    199200    property ThreadPoolSize: Integer read GetThreadPoolSize
    200201      write SetThreadPoolSize;
     
    216217  MicroThreadBlockStateText: array[TMicroThreadBlockState] of string = ('None',
    217218    'Sleeping', 'WaitFor', 'Terminating', 'Terminated');
     219  MicroThreadThreadStateText: array[TMicroThreadThreadState] of string = (
     220    'Ready', 'Running', 'Terminated');
    218221
    219222function GetCurrentMicroThread: TMicroThread;
     
    503506  try
    504507    repeat
    505       ExecutedCount := Manager.Execute(100000);
     508      State := ttsRunning;
     509      ExecutedCount := Manager.Execute(10);
     510      State := ttsReady;
    506511      if ExecutedCount = 0 then Sleep(1);
    507512    until Terminated;
     
    516521begin
    517522  inherited;
    518   ExecuteTerminated := False;
     523  State := ttsReady;
    519524  Manager := TMicroThreadManager.Create;
    520525end;
     
    522527destructor TMicroThreadThread.Destroy;
    523528begin
    524   Terminate;
    525   repeat
    526     Sleep(1);
    527   until ExecuteTerminated;
    528 
    529529  Manager.Free;
    530530  inherited Destroy;
     
    758758begin
    759759  FState := ssTerminating;
     760  // Wait for all thread managers to finish
    760761  try
    761762    FThreadPoolLock.Acquire;
     
    763764      TMicroThreadThread(FThreadPool[I]).Terminate;
    764765    end;
     766    for I := 0 to FThreadPool.Count - 1 do begin
     767      TMicroThreadThread(FThreadPool[I]).WaitFor;
     768    end;
     769    FThreadPool.Clear;
    765770  finally
    766771    FThreadPoolLock.Release;
    767772  end;
    768773
    769   // Wait for all thread managers to finish
    770774  repeat
    771775    Application.ProcessMessages;
    772776    Sleep(1);
    773   until FMainThreadTerminated and (ThreadPoolCount = 0);
     777  until FMainThreadTerminated or (not FUseMainThread);
    774778  FState := ssStopped;
    775 end;
    776 
    777 procedure TMicroThreadScheduler.PoolThreadTerminated(Sender: TObject);
    778 var
    779   ThreadIndex: Integer;
    780 begin
    781   TMicroThreadThread(Sender).ExecuteTerminated := True;
    782   try
    783     FThreadPoolLock.Acquire;
    784     FThreadPool.OwnsObjects := False;
    785     ThreadIndex := FThreadPool.IndexOf(Sender);
    786     if ThreadIndex = -1 then
    787       raise Exception.Create('Trying to free thread not found in thread pool');
    788     FThreadPool.Delete(ThreadIndex);
    789   finally
    790     FThreadPool.OwnsObjects := True;
    791     FThreadPoolLock.Release;
    792   end;
    793779end;
    794780
     
    806792        NewThread.Manager.FId := FThreadPool.Count + 1;
    807793        NewThread.Manager.FThread := NewThread;
    808         NewThread.OnTerminate := PoolThreadTerminated;
    809         NewThread.FreeOnTerminate := True;
     794        //NewThread.OnTerminate := PoolThreadTerminated;
     795        NewThread.FreeOnTerminate := False;
    810796        ThreadPool.Add(NewThread);
    811797        NewThread.Resume;
     
    813799    end else begin
    814800      while FThreadPool.Count > FThreadPoolSize do begin
     801        TMicroThreadThread(FThreadPool[FThreadPool.Count - 1]).Terminate;
     802        TMicroThreadThread(FThreadPool[FThreadPool.Count - 1]).WaitFor;
    815803        FThreadPool.Delete(FThreadPool.Count - 1);
    816804      end;
     
    946934
    947935procedure TMicroThreadScheduler.SetThreadPoolSize(const AValue: Integer);
    948 var
    949   I: Integer;
    950   NewThread: TMicroThreadThread;
    951936begin
    952937  FThreadPoolSize := AValue;
Note: See TracChangeset for help on using the changeset viewer.