Changeset 157


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.
Location:
MicroThreading
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • MicroThreading/Demo/Demo.lpi

    r156 r157  
    4747        <TopLine Value="1"/>
    4848        <CursorPos X="34" Y="16"/>
    49         <UsageCount Value="82"/>
     49        <UsageCount Value="86"/>
    5050      </Unit0>
    5151      <Unit1>
     
    5555        <ResourceBaseClass Value="Form"/>
    5656        <UnitName Value="UMainForm"/>
    57         <EditorIndex Value="9"/>
    58         <WindowIndex Value="0"/>
    59         <TopLine Value="270"/>
    60         <CursorPos X="1" Y="284"/>
    61         <UsageCount Value="82"/>
     57        <EditorIndex Value="4"/>
     58        <WindowIndex Value="0"/>
     59        <TopLine Value="247"/>
     60        <CursorPos X="1" Y="261"/>
     61        <UsageCount Value="86"/>
    6262        <Loaded Value="True"/>
    6363        <LoadedDesigner Value="True"/>
     
    6969        <EditorIndex Value="0"/>
    7070        <WindowIndex Value="0"/>
    71         <TopLine Value="513"/>
    72         <CursorPos X="9" Y="525"/>
    73         <UsageCount Value="43"/>
     71        <TopLine Value="175"/>
     72        <CursorPos X="1" Y="190"/>
     73        <UsageCount Value="45"/>
    7474        <Loaded Value="True"/>
    7575      </Unit2>
     
    142142      <Unit11>
    143143        <Filename Value="/usr/share/fpcsrc/2.4.0/rtl/inc/systemh.inc"/>
    144         <WindowIndex Value="0"/>
    145         <TopLine Value="479"/>
    146         <CursorPos X="3" Y="494"/>
    147         <UsageCount Value="9"/>
     144        <EditorIndex Value="1"/>
     145        <WindowIndex Value="0"/>
     146        <TopLine Value="896"/>
     147        <CursorPos X="10" Y="911"/>
     148        <UsageCount Value="10"/>
     149        <Loaded Value="True"/>
    148150      </Unit11>
    149151      <Unit12>
     
    192194      <Unit18>
    193195        <Filename Value="/usr/share/fpcsrc/2.4.0/rtl/objpas/classes/classesh.inc"/>
    194         <WindowIndex Value="0"/>
    195         <TopLine Value="1823"/>
    196         <CursorPos X="3" Y="1838"/>
     196        <EditorIndex Value="2"/>
     197        <WindowIndex Value="0"/>
     198        <TopLine Value="1"/>
     199        <CursorPos X="50" Y="9"/>
    197200        <UsageCount Value="10"/>
     201        <Loaded Value="True"/>
    198202      </Unit18>
    199203      <Unit19>
     
    257261      <Unit27>
    258262        <Filename Value="../../../../Programy/Lazarus/0.9.31_2.4.3/fpc/2.4.3/source/rtl/win/wininc/redef.inc"/>
    259         <EditorIndex Value="7"/>
    260263        <WindowIndex Value="0"/>
    261264        <TopLine Value="739"/>
    262265        <CursorPos X="27" Y="752"/>
    263266        <UsageCount Value="17"/>
    264         <Loaded Value="True"/>
    265267      </Unit27>
    266268      <Unit28>
    267269        <Filename Value="../../../../Programy/Lazarus/0.9.31_2.4.3/fpc/2.4.3/source/packages/fcl-base/src/syncobjs.pp"/>
    268270        <UnitName Value="syncobjs"/>
    269         <EditorIndex Value="2"/>
    270271        <WindowIndex Value="0"/>
    271272        <TopLine Value="104"/>
    272273        <CursorPos X="28" Y="106"/>
    273274        <UsageCount Value="14"/>
    274         <Loaded Value="True"/>
    275275      </Unit28>
    276276      <Unit29>
    277277        <Filename Value="../../../../Programy/Lazarus/0.9.31_2.4.3/fpc/2.4.3/source/rtl/inc/threadh.inc"/>
    278         <EditorIndex Value="3"/>
    279278        <WindowIndex Value="0"/>
    280279        <TopLine Value="138"/>
    281280        <CursorPos X="11" Y="151"/>
    282281        <UsageCount Value="14"/>
    283         <Loaded Value="True"/>
    284282      </Unit29>
    285283      <Unit30>
    286284        <Filename Value="../../../../Programy/Lazarus/0.9.31_2.4.3/fpc/2.4.3/source/rtl/inc/thread.inc"/>
    287         <EditorIndex Value="4"/>
    288285        <WindowIndex Value="0"/>
    289286        <TopLine Value="199"/>
    290287        <CursorPos X="3" Y="202"/>
    291288        <UsageCount Value="14"/>
    292         <Loaded Value="True"/>
    293289      </Unit30>
    294290      <Unit31>
    295291        <Filename Value="../../../../Programy/Lazarus/0.9.31_2.4.3/fpc/2.4.3/source/rtl/win/sysosh.inc"/>
    296         <EditorIndex Value="12"/>
    297292        <WindowIndex Value="0"/>
    298293        <TopLine Value="8"/>
    299294        <CursorPos X="3" Y="33"/>
    300295        <UsageCount Value="14"/>
    301         <Loaded Value="True"/>
    302296      </Unit31>
    303297      <Unit32>
    304298        <Filename Value="../../../../Programy/Lazarus/0.9.31_2.4.3/fpc/2.4.3/source/rtl/objpas/classes/classesh.inc"/>
    305         <EditorIndex Value="10"/>
    306299        <WindowIndex Value="0"/>
    307300        <TopLine Value="302"/>
    308301        <CursorPos X="19" Y="319"/>
    309302        <UsageCount Value="20"/>
    310         <Loaded Value="True"/>
    311303      </Unit32>
    312304      <Unit33>
     
    396388      <Unit45>
    397389        <Filename Value="../../../../Programy/Lazarus/0.9.31_2.4.3/lcl/interfaces/win32/win32object.inc"/>
    398         <EditorIndex Value="1"/>
    399390        <WindowIndex Value="0"/>
    400391        <TopLine Value="387"/>
    401392        <CursorPos X="1" Y="400"/>
    402393        <UsageCount Value="11"/>
    403         <Loaded Value="True"/>
    404394      </Unit45>
    405395      <Unit46>
     
    420410        <Filename Value="../UPlatform.pas"/>
    421411        <UnitName Value="UPlatform"/>
    422         <EditorIndex Value="6"/>
     412        <EditorIndex Value="3"/>
    423413        <WindowIndex Value="0"/>
    424414        <TopLine Value="23"/>
    425415        <CursorPos X="31" Y="38"/>
    426         <UsageCount Value="23"/>
     416        <UsageCount Value="25"/>
    427417        <Loaded Value="True"/>
    428418      </Unit48>
     
    487477      <Unit57>
    488478        <Filename Value="../../../../Programy/Lazarus/0.9.31_2.4.3/fpc/2.4.3/source/rtl/objpas/classes/lists.inc"/>
    489         <EditorIndex Value="11"/>
    490479        <WindowIndex Value="0"/>
    491480        <TopLine Value="909"/>
    492481        <CursorPos X="26" Y="917"/>
    493482        <UsageCount Value="15"/>
    494         <Loaded Value="True"/>
    495483      </Unit57>
    496484      <Unit58>
     
    535523      <Unit63>
    536524        <Filename Value="../../../../Programy/Lazarus/0.9.31_2.4.3/fpc/2.4.3/source/rtl/objpas/sysutils/sysutilh.inc"/>
    537         <EditorIndex Value="8"/>
    538525        <WindowIndex Value="0"/>
    539526        <TopLine Value="1"/>
    540527        <CursorPos X="55" Y="4"/>
    541528        <UsageCount Value="16"/>
    542         <Loaded Value="True"/>
    543529      </Unit63>
    544530      <Unit64>
    545531        <Filename Value="../../../../Programy/Lazarus/0.9.31_2.4.3/lcl/controls.pp"/>
    546532        <UnitName Value="Controls"/>
    547         <EditorIndex Value="5"/>
    548533        <WindowIndex Value="0"/>
    549534        <TopLine Value="2660"/>
    550535        <CursorPos X="1" Y="2673"/>
    551536        <UsageCount Value="15"/>
    552         <Loaded Value="True"/>
    553537      </Unit64>
    554538    </Units>
    555     <JumpHistory Count="30" HistoryIndex="28">
     539    <JumpHistory Count="30" HistoryIndex="29">
    556540      <Position1>
    557541        <Filename Value="../UMicroThreading.pas"/>
    558         <Caret Line="773" Column="1" TopLine="760"/>
     542        <Caret Line="531" Column="1" TopLine="515"/>
    559543      </Position1>
    560544      <Position2>
    561545        <Filename Value="../UMicroThreading.pas"/>
    562         <Caret Line="774" Column="1" TopLine="760"/>
     546        <Caret Line="532" Column="1" TopLine="515"/>
    563547      </Position2>
    564548      <Position3>
    565549        <Filename Value="../UMicroThreading.pas"/>
    566         <Caret Line="772" Column="1" TopLine="760"/>
     550        <Caret Line="954" Column="1" TopLine="939"/>
    567551      </Position3>
    568552      <Position4>
    569553        <Filename Value="../UMicroThreading.pas"/>
    570         <Caret Line="773" Column="1" TopLine="760"/>
     554        <Caret Line="822" Column="1" TopLine="807"/>
    571555      </Position4>
    572556      <Position5>
    573557        <Filename Value="../UMicroThreading.pas"/>
    574         <Caret Line="774" Column="1" TopLine="760"/>
     558        <Caret Line="824" Column="1" TopLine="807"/>
    575559      </Position5>
    576560      <Position6>
    577561        <Filename Value="../UMicroThreading.pas"/>
    578         <Caret Line="772" Column="1" TopLine="760"/>
     562        <Caret Line="955" Column="1" TopLine="940"/>
    579563      </Position6>
    580564      <Position7>
    581         <Filename Value="../UMicroThreading.pas"/>
    582         <Caret Line="773" Column="1" TopLine="760"/>
     565        <Filename Value="UMainForm.pas"/>
     566        <Caret Line="262" Column="1" TopLine="247"/>
    583567      </Position7>
    584568      <Position8>
    585         <Filename Value="../UMicroThreading.pas"/>
    586         <Caret Line="774" Column="1" TopLine="760"/>
     569        <Filename Value="UMainForm.pas"/>
     570        <Caret Line="261" Column="1" TopLine="247"/>
    587571      </Position8>
    588572      <Position9>
    589573        <Filename Value="../UMicroThreading.pas"/>
    590         <Caret Line="772" Column="1" TopLine="760"/>
     574        <Caret Line="816" Column="1" TopLine="801"/>
    591575      </Position9>
    592576      <Position10>
    593577        <Filename Value="../UMicroThreading.pas"/>
    594         <Caret Line="773" Column="1" TopLine="760"/>
     578        <Caret Line="777" Column="1" TopLine="756"/>
    595579      </Position10>
    596580      <Position11>
    597581        <Filename Value="../UMicroThreading.pas"/>
    598         <Caret Line="774" Column="1" TopLine="760"/>
     582        <Caret Line="760" Column="1" TopLine="754"/>
    599583      </Position11>
    600584      <Position12>
    601585        <Filename Value="../UMicroThreading.pas"/>
    602         <Caret Line="772" Column="1" TopLine="760"/>
     586        <Caret Line="762" Column="1" TopLine="754"/>
    603587      </Position12>
    604588      <Position13>
    605589        <Filename Value="../UMicroThreading.pas"/>
    606         <Caret Line="773" Column="1" TopLine="760"/>
     590        <Caret Line="763" Column="1" TopLine="754"/>
    607591      </Position13>
    608592      <Position14>
    609593        <Filename Value="../UMicroThreading.pas"/>
    610         <Caret Line="774" Column="1" TopLine="760"/>
     594        <Caret Line="764" Column="1" TopLine="754"/>
    611595      </Position14>
    612596      <Position15>
    613597        <Filename Value="../UMicroThreading.pas"/>
    614         <Caret Line="772" Column="1" TopLine="760"/>
     598        <Caret Line="768" Column="2" TopLine="754"/>
    615599      </Position15>
    616600      <Position16>
    617601        <Filename Value="../UMicroThreading.pas"/>
    618         <Caret Line="773" Column="1" TopLine="760"/>
     602        <Caret Line="767" Column="1" TopLine="754"/>
    619603      </Position16>
    620604      <Position17>
    621605        <Filename Value="../UMicroThreading.pas"/>
    622         <Caret Line="774" Column="1" TopLine="760"/>
     606        <Caret Line="768" Column="1" TopLine="754"/>
    623607      </Position17>
    624608      <Position18>
    625609        <Filename Value="../UMicroThreading.pas"/>
    626         <Caret Line="772" Column="1" TopLine="760"/>
     610        <Caret Line="769" Column="1" TopLine="754"/>
    627611      </Position18>
    628612      <Position19>
    629613        <Filename Value="../UMicroThreading.pas"/>
    630         <Caret Line="773" Column="1" TopLine="760"/>
     614        <Caret Line="768" Column="1" TopLine="754"/>
    631615      </Position19>
    632616      <Position20>
    633617        <Filename Value="../UMicroThreading.pas"/>
    634         <Caret Line="774" Column="1" TopLine="760"/>
     618        <Caret Line="770" Column="22" TopLine="754"/>
    635619      </Position20>
    636620      <Position21>
    637621        <Filename Value="../UMicroThreading.pas"/>
    638         <Caret Line="772" Column="1" TopLine="760"/>
     622        <Caret Line="760" Column="1" TopLine="754"/>
    639623      </Position21>
    640624      <Position22>
    641625        <Filename Value="../UMicroThreading.pas"/>
    642         <Caret Line="773" Column="1" TopLine="760"/>
     626        <Caret Line="762" Column="1" TopLine="754"/>
    643627      </Position22>
    644628      <Position23>
    645629        <Filename Value="../UMicroThreading.pas"/>
    646         <Caret Line="774" Column="1" TopLine="760"/>
     630        <Caret Line="767" Column="1" TopLine="754"/>
    647631      </Position23>
    648632      <Position24>
    649633        <Filename Value="../UMicroThreading.pas"/>
    650         <Caret Line="810" Column="35" TopLine="796"/>
     634        <Caret Line="768" Column="1" TopLine="754"/>
    651635      </Position24>
    652636      <Position25>
    653         <Filename Value="../../../../Programy/Lazarus/0.9.31_2.4.3/lcl/interfaces/win32/win32object.inc"/>
    654         <Caret Line="400" Column="1" TopLine="387"/>
     637        <Filename Value="../UMicroThreading.pas"/>
     638        <Caret Line="770" Column="1" TopLine="754"/>
    655639      </Position25>
    656640      <Position26>
    657641        <Filename Value="../UMicroThreading.pas"/>
    658         <Caret Line="524" Column="9" TopLine="513"/>
     642        <Caret Line="772" Column="1" TopLine="754"/>
    659643      </Position26>
    660644      <Position27>
    661645        <Filename Value="../UMicroThreading.pas"/>
    662         <Caret Line="523" Column="1" TopLine="513"/>
     646        <Caret Line="776" Column="1" TopLine="754"/>
    663647      </Position27>
    664648      <Position28>
    665649        <Filename Value="../UMicroThreading.pas"/>
    666         <Caret Line="524" Column="1" TopLine="513"/>
     650        <Caret Line="777" Column="1" TopLine="754"/>
    667651      </Position28>
    668652      <Position29>
    669653        <Filename Value="../UMicroThreading.pas"/>
    670         <Caret Line="526" Column="1" TopLine="513"/>
     654        <Caret Line="778" Column="1" TopLine="754"/>
    671655      </Position29>
    672656      <Position30>
    673         <Filename Value="../../../../Programy/Lazarus/0.9.31_2.4.3/lcl/interfaces/win32/win32object.inc"/>
    674         <Caret Line="400" Column="1" TopLine="387"/>
     657        <Filename Value="../UMicroThreading.pas"/>
     658        <Caret Line="782" Column="1" TopLine="782"/>
    675659      </Position30>
    676660    </JumpHistory>
     
    712696  </CompilerOptions>
    713697  <Debugging>
    714     <BreakPoints Count="1">
    715       <Item1>
    716         <Source Value="../UMicroThreading.pas"/>
    717         <Line Value="523"/>
    718       </Item1>
    719     </BreakPoints>
    720698    <Watches Count="2">
    721699      <Item1>
  • MicroThreading/Demo/UMainForm.lfm

    r156 r157  
    2222    object TabSheet2: TTabSheet
    2323      Caption = 'Scheduler state'
    24       ClientHeight = 493
    25       ClientWidth = 794
     24      ClientHeight = 492
     25      ClientWidth = 798
    2626      object ListView1: TListView
    2727        Left = 222
    28         Height = 460
     28        Height = 459
    2929        Top = 24
    30         Width = 567
     30        Width = 571
    3131        Anchors = [akTop, akLeft, akRight, akBottom]
    3232        Columns = <       
     
    7676        Height = 14
    7777        Top = 7
    78         Width = 74
    79         Caption = 'Microthread list'
     78        Width = 76
     79        Caption = 'Microthreads:'
    8080        ParentColor = False
    8181      end
     
    8686        Width = 205
    8787        Caption = 'State'
    88         ClientHeight = 63
     88        ClientHeight = 66
    8989        ClientWidth = 201
    9090        TabOrder = 1
     
    9393          Height = 14
    9494          Top = 4
    95           Width = 117
     95          Width = 135
    9696          Caption = 'Logical processor count:'
    9797          ParentColor = False
     
    109109          Height = 14
    110110          Top = 20
    111           Width = 69
     111          Width = 78
    112112          Caption = 'Thread count:'
    113113          ParentColor = False
     
    117117          Height = 14
    118118          Top = 36
    119           Width = 95
     119          Width = 107
    120120          Caption = 'Micro thread count:'
    121121          ParentColor = False
     
    144144        Width = 206
    145145        Caption = 'Control'
    146         ClientHeight = 108
     146        ClientHeight = 111
    147147        ClientWidth = 202
    148148        TabOrder = 2
     
    160160          Height = 14
    161161          Top = 46
    162           Width = 69
     162          Width = 78
    163163          Caption = 'Thread count:'
    164164          ParentColor = False
     
    174174        object CheckBoxUseMainThread: TCheckBox
    175175          Left = 10
    176           Height = 17
     176          Height = 20
    177177          Top = 64
    178           Width = 98
     178          Width = 112
    179179          Caption = 'Use main thread'
    180180          OnChange = CheckBoxUseMainThreadChange
     
    182182        end
    183183      end
     184      object ListView2: TListView
     185        Left = 6
     186        Height = 236
     187        Top = 247
     188        Width = 206
     189        Anchors = [akTop, akLeft, akBottom]
     190        Columns = <       
     191          item
     192            Caption = 'Id'
     193          end       
     194          item
     195            Caption = 'State'
     196            Width = 135
     197          end>
     198        OwnerData = True
     199        TabOrder = 3
     200        ViewStyle = vsReport
     201        OnData = ListView2Data
     202      end
     203      object Label13: TLabel
     204        Left = 7
     205        Height = 14
     206        Top = 233
     207        Width = 50
     208        Caption = 'Threads:'
     209        ParentColor = False
     210      end
    184211    end
    185212    object TabSheet1: TTabSheet
    186213      Caption = 'Job control'
    187       ClientHeight = 493
    188       ClientWidth = 794
     214      ClientHeight = 492
     215      ClientWidth = 798
    189216      object ButtonAddWorkers: TButton
    190217        Left = 10
     
    263290    object TabSheet3: TTabSheet
    264291      Caption = 'Testing'
    265       ClientHeight = 493
    266       ClientWidth = 753
     292      ClientHeight = 492
     293      ClientWidth = 798
    267294      object ButtonShowThreadId: TButton
    268295        Left = 14
  • MicroThreading/Demo/UMainForm.pas

    r156 r157  
    2828    Label11: TLabel;
    2929    Label12: TLabel;
     30    Label13: TLabel;
    3031    Label2: TLabel;
    3132    Label3: TLabel;
     
    3738    Label9: TLabel;
    3839    ListView1: TListView;
     40    ListView2: TListView;
    3941    Memo1: TMemo;
    4042    PageControl1: TPageControl;
     
    5860    procedure FormShow(Sender: TObject);
    5961    procedure ListView1Data(Sender: TObject; Item: TListItem);
     62    procedure ListView2Data(Sender: TObject; Item: TListItem);
    6063    procedure SpinEdit2Change(Sender: TObject);
    6164    procedure SpinEdit3Change(Sender: TObject);
     
    240243end;
    241244
     245procedure TMainForm.ListView2Data(Sender: TObject; Item: TListItem);
     246begin
     247  if Item.Index < MainScheduler.ThreadPoolCount then
     248  try
     249    MainScheduler.ThreadPoolLock.Acquire;
     250    with TMicroThreadThread(MainScheduler.ThreadPool[Item.Index]) do begin
     251      Item.Caption := IntToStr(ThreadID);
     252      Item.SubItems.Add(MicroThreadThreadStateText[State]);
     253    end;
     254  finally
     255    MainScheduler.ThreadPoolLock.Release;
     256  end;
     257end;
     258
    242259procedure TMainForm.SpinEdit2Change(Sender: TObject);
    243260begin
     
    256273  ListView1.Items[-1];
    257274  ListView1.Refresh;
     275
     276  if ListView2.Items.Count <> MainScheduler.ThreadPoolCount then
     277    ListView2.Items.Count := MainScheduler.ThreadPoolCount;
     278  ListView2.Items[-1];
     279  ListView2.Refresh;
     280
    258281  Label2.Caption := DateTimeToStr(NowPrecise) + ' ' +
    259282    FloatToStr(Frac(NowPrecise / OneSecond));
  • 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.