Subject | Re: Multi-thread issues |
---|---|
Author | personalsoft_fabiano |
Post date | 2009-01-03T03:01:49Z |
I rewrote the test application to simplify it, and i´m posting the
code and the result times here.
I put the database in a remote server to get larger roundtrip times.
In this test, 15 created threads are finishing in random order as
expected, but it seems there is always an aprox. delay of 500-600 ms
between each thread finish.
The first thread terminates in 14 seconds, and i presume it is the
aprox. time needed to connect to the database, to execute the query in
the server and to close the connection.
But it takes aprox 24 seconds to terminate the last thread.
As all threads are running in paralel, shouldn't all threads finish in
aprox. 14 seconds also?
TEST APPLICATION:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,
Forms,
Dialogs, IB_Components, IBODataSet, StdCtrls, SyncObjs;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
type
TTeste = class(TThread)
public
procedure Execute; override;
end;
{$R *.dfm}
var
C: TCriticalSection;
F: TextFile;
Count: Integer = 0;
{ TTeste }
procedure TTeste.Execute;
var
IB_Session: TIB_Session;
IB_Connection: TIB_Connection;
IB_Transaction: TIB_Transaction;
IBOQuery: TIBOQuery;
I: Integer;
S: String;
begin
C.Acquire;
Inc(Count);
I := Count;
C.Release;
C.Acquire;
S := FormatDateTime(IntToStr(I) + ' - hh:nn:ss.zzz', Now);
Writeln(F, S);
C.Release;
IB_Session := TIB_Session.Create(nil);
IB_Connection := TIB_Connection.Create(IB_Session);
IB_Connection.DatabaseName := 'myserver.com:modelo';
IB_Connection.Username := 'sysdba';
IB_Connection.Password := 'masterkey';
IB_Connection.IB_Session := IB_Session;
IB_Connection.CacheStatementHandles := False;
IB_Connection.Open;
IB_Transaction := TIB_Transaction.Create(IB_Session);
IB_Transaction.IB_Session := IB_Session;
IB_Transaction.IB_Connection := IB_Connection;
IB_Transaction.StartTransaction;
IBOQuery := TIBOQuery.Create(IB_Session);
IBOQuery.IB_Session := IB_Session;
IBOQuery.IB_Connection := IB_Connection;
IBOQuery.IB_Transaction := IB_Transaction;
IBOQuery.SQL.Text := 'select * from rdb$database';
IBOQuery.Open;
IBOQuery.Close;
IB_Session.Free;
C.Acquire;
S := FormatDateTime(IntToStr(I) + ' - hh:nn:ss.zzz', Now);
Writeln(F, S);
C.Release;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
I: Integer;
begin
for I := 1 to 15 do
TTeste.Create(False);
end;
initialization
C := TCriticalSection.Create;
AssignFile(F, 'c:\log.txt');
Rewrite(F);
finalization
CloseFile(F);
end.
FIRST RESULT:
1 - 03:57:48.862
2 - 03:57:48.878
3 - 03:57:48.878
4 - 03:57:48.879
5 - 03:57:48.913
6 - 03:57:48.913
7 - 03:57:48.913
8 - 03:57:48.913
9 - 03:57:48.918
10 - 03:57:48.919
11 - 03:57:48.919
12 - 03:57:48.919
13 - 03:57:48.919
14 - 03:57:48.919
15 - 03:57:48.919
1 - 03:58:02.357
12 - 03:58:02.790
2 - 03:58:03.251
4 - 03:58:03.663
14 - 03:58:04.071
10 - 03:58:04.396
9 - 03:58:04.717
11 - 03:58:04.979
13 - 03:58:05.313
8 - 03:58:06.174
5 - 03:58:07.187
7 - 03:58:08.314
3 - 03:58:09.611
15 - 03:58:09.868
6 - 03:58:11.546
SECOND RESULT:
1 - 03:55:37.716
2 - 03:55:37.717
3 - 03:55:37.718
4 - 03:55:37.737
5 - 03:55:37.737
6 - 03:55:37.737
7 - 03:55:37.738
8 - 03:55:37.738
9 - 03:55:37.739
10 - 03:55:37.739
11 - 03:55:37.739
12 - 03:55:37.739
13 - 03:55:37.740
14 - 03:55:37.740
15 - 03:55:37.740
6 - 03:55:51.307
15 - 03:55:52.045
14 - 03:55:52.547
8 - 03:55:52.977
7 - 03:55:53.451
5 - 03:55:53.884
3 - 03:55:54.296
1 - 03:55:54.665
2 - 03:55:55.395
4 - 03:55:56.361
10 - 03:55:57.397
11 - 03:55:58.485
12 - 03:55:59.870
9 - 03:56:00.424
13 - 03:56:01.732
Regards,
Fabiano
code and the result times here.
I put the database in a remote server to get larger roundtrip times.
In this test, 15 created threads are finishing in random order as
expected, but it seems there is always an aprox. delay of 500-600 ms
between each thread finish.
The first thread terminates in 14 seconds, and i presume it is the
aprox. time needed to connect to the database, to execute the query in
the server and to close the connection.
But it takes aprox 24 seconds to terminate the last thread.
As all threads are running in paralel, shouldn't all threads finish in
aprox. 14 seconds also?
TEST APPLICATION:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,
Forms,
Dialogs, IB_Components, IBODataSet, StdCtrls, SyncObjs;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
type
TTeste = class(TThread)
public
procedure Execute; override;
end;
{$R *.dfm}
var
C: TCriticalSection;
F: TextFile;
Count: Integer = 0;
{ TTeste }
procedure TTeste.Execute;
var
IB_Session: TIB_Session;
IB_Connection: TIB_Connection;
IB_Transaction: TIB_Transaction;
IBOQuery: TIBOQuery;
I: Integer;
S: String;
begin
C.Acquire;
Inc(Count);
I := Count;
C.Release;
C.Acquire;
S := FormatDateTime(IntToStr(I) + ' - hh:nn:ss.zzz', Now);
Writeln(F, S);
C.Release;
IB_Session := TIB_Session.Create(nil);
IB_Connection := TIB_Connection.Create(IB_Session);
IB_Connection.DatabaseName := 'myserver.com:modelo';
IB_Connection.Username := 'sysdba';
IB_Connection.Password := 'masterkey';
IB_Connection.IB_Session := IB_Session;
IB_Connection.CacheStatementHandles := False;
IB_Connection.Open;
IB_Transaction := TIB_Transaction.Create(IB_Session);
IB_Transaction.IB_Session := IB_Session;
IB_Transaction.IB_Connection := IB_Connection;
IB_Transaction.StartTransaction;
IBOQuery := TIBOQuery.Create(IB_Session);
IBOQuery.IB_Session := IB_Session;
IBOQuery.IB_Connection := IB_Connection;
IBOQuery.IB_Transaction := IB_Transaction;
IBOQuery.SQL.Text := 'select * from rdb$database';
IBOQuery.Open;
IBOQuery.Close;
IB_Session.Free;
C.Acquire;
S := FormatDateTime(IntToStr(I) + ' - hh:nn:ss.zzz', Now);
Writeln(F, S);
C.Release;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
I: Integer;
begin
for I := 1 to 15 do
TTeste.Create(False);
end;
initialization
C := TCriticalSection.Create;
AssignFile(F, 'c:\log.txt');
Rewrite(F);
finalization
CloseFile(F);
end.
FIRST RESULT:
1 - 03:57:48.862
2 - 03:57:48.878
3 - 03:57:48.878
4 - 03:57:48.879
5 - 03:57:48.913
6 - 03:57:48.913
7 - 03:57:48.913
8 - 03:57:48.913
9 - 03:57:48.918
10 - 03:57:48.919
11 - 03:57:48.919
12 - 03:57:48.919
13 - 03:57:48.919
14 - 03:57:48.919
15 - 03:57:48.919
1 - 03:58:02.357
12 - 03:58:02.790
2 - 03:58:03.251
4 - 03:58:03.663
14 - 03:58:04.071
10 - 03:58:04.396
9 - 03:58:04.717
11 - 03:58:04.979
13 - 03:58:05.313
8 - 03:58:06.174
5 - 03:58:07.187
7 - 03:58:08.314
3 - 03:58:09.611
15 - 03:58:09.868
6 - 03:58:11.546
SECOND RESULT:
1 - 03:55:37.716
2 - 03:55:37.717
3 - 03:55:37.718
4 - 03:55:37.737
5 - 03:55:37.737
6 - 03:55:37.737
7 - 03:55:37.738
8 - 03:55:37.738
9 - 03:55:37.739
10 - 03:55:37.739
11 - 03:55:37.739
12 - 03:55:37.739
13 - 03:55:37.740
14 - 03:55:37.740
15 - 03:55:37.740
6 - 03:55:51.307
15 - 03:55:52.045
14 - 03:55:52.547
8 - 03:55:52.977
7 - 03:55:53.451
5 - 03:55:53.884
3 - 03:55:54.296
1 - 03:55:54.665
2 - 03:55:55.395
4 - 03:55:56.361
10 - 03:55:57.397
11 - 03:55:58.485
12 - 03:55:59.870
9 - 03:56:00.424
13 - 03:56:01.732
Regards,
Fabiano