I am creating a console application that needs to execute multiple threads to complete a task. My problem is that threads start one after another (thread1 start → work → end and ONLY and then start thread2) instead of starting everything at the same time. Also, I do not want more than 10 threads to work at the same time (performance issues). Bellow is an example of console application code and used datamodule. my application works the same way. I used the datamodule because after the threads have finished, I have to populate the database with this data. There are also comments in the code to explain what is the reason for something.
application console code:
program Project2; {$APPTYPE CONSOLE} uses SysUtils, Unit1 in 'Unit1.pas' {DataModule1: TDataModule}; var dm:TDataModule1; begin dm:=TDataModule1.Create(nil); try dm.execute; finally FreeAndNil(dm); end; end.
and datamodule code
unit Unit1; interface uses SysUtils, Classes, SyncObjs, Windows, Forms; var FCritical: TRTLCriticalSection;//accessing the global variables type TTestThread = class(TThread) protected procedure Execute;override; end; TDataModule1 = class(TDataModule) procedure DataModuleCreate(Sender: TObject); procedure DataModuleDestroy(Sender: TObject); private { Déclarations privées } public procedure execute; procedure CreateThread(); procedure Onterminatethrd(Sender: TObject); end; var DataModule1 : TDataModule1; FthreadCount : Integer; //know how many threads are running implementation {$R *.dfm} { TTestThread } procedure TTestThread.Execute; var f : TextFile; i : integer; begin EnterCriticalSection(fcritical); AssignFile(f, 'd:\a' + inttostr(FthreadCount) + '.txt'); LeaveCriticalSection(fcritical); Rewrite(f); try i := 0; while i <= 1000000 do // do some work... Inc(i); Writeln(f, 'done'); finally CloseFile(f); end; end; { TDataModule1 } procedure TDataModule1.CreateThread; var aThrd : TTestThread; begin aThrd := TTestThread.Create(True); aThrd.FreeOnTerminate := True; EnterCriticalSection(fcritical); Inc(FthreadCount); LeaveCriticalSection(fcritical); aThrd.OnTerminate:=Onterminatethrd; try aThrd.Resume; except FreeAndNil(aThrd); end; end; procedure TDataModule1.Onterminatethrd(Sender: TObject); begin EnterCriticalSection(fcritical); Dec(FthreadCount); LeaveCriticalSection(fcritical); end; procedure TDataModule1.DataModuleCreate(Sender: TObject); begin InitializeCriticalSection(fcritical); end; procedure TDataModule1.DataModuleDestroy(Sender: TObject); begin DeleteCriticalSection(fcritical); end; procedure TDataModule1.execute; var i : integer; begin i := 0; while i < 1000 do begin while (FthreadCount = 10) do Application.ProcessMessages;//wait for an thread to finish. max threads at a //time =10 CreateThread; EnterCriticalSection(fcritical); Inc(i); LeaveCriticalSection(fcritical); while FthreadCount > 0 do //wait for all threads to finish in order to close the //main thread begin Application.ProcessMessages; CheckSynchronize; end; end; end; end.
since I said that the problem is that my threads are working one by one, and not all are working at the same time. I also saw that sometimes only the first thread worked, after which everything else was simply created and ended. in my application all code is protected by try-excepts, but no errors occur.
Can someone give me some advice?
multithreading delphi critical-section
Rba
source share