examples: fixed multithreading examples compilation with trunk FPC (2.5.1). Patch from alexs.

git-svn-id: trunk@24054 -
This commit is contained in:
maxim 2010-03-16 21:52:31 +00:00
parent 66978feef5
commit 6ec276af67
12 changed files with 118 additions and 90 deletions

View File

@ -1,14 +1,18 @@
<?xml version="1.0"?> <?xml version="1.0"?>
<CONFIG> <CONFIG>
<ProjectOptions> <ProjectOptions>
<PathDelim Value="/"/> <Version Value="7"/>
<Version Value="6"/>
<General> <General>
<Flags>
<LRSInOutputDirectory Value="False"/>
</Flags>
<SessionStorage Value="InProjectDir"/> <SessionStorage Value="InProjectDir"/>
<MainUnit Value="0"/> <MainUnit Value="0"/>
<IconPath Value="./"/>
<TargetFileExt Value=""/> <TargetFileExt Value=""/>
</General> </General>
<VersionInfo>
<StringTable Comments="" CompanyName="" FileDescription="" FileVersion="0.0.0.0" InternalName="" LegalCopyright="" LegalTrademarks="" OriginalFilename="" ProductName="" ProductVersion="0.0.0.0"/>
</VersionInfo>
<PublishOptions> <PublishOptions>
<Version Value="2"/> <Version Value="2"/>
<IgnoreBinaries Value="False"/> <IgnoreBinaries Value="False"/>
@ -34,9 +38,9 @@
</Unit0> </Unit0>
<Unit1> <Unit1>
<Filename Value="criticalsectionunit1.pas"/> <Filename Value="criticalsectionunit1.pas"/>
<ComponentName Value="Form1"/>
<IsPartOfProject Value="True"/> <IsPartOfProject Value="True"/>
<ResourceFilename Value="criticalsectionunit1.lrs"/> <ComponentName Value="Form1"/>
<ResourceBaseClass Value="Form"/>
<UnitName Value="CriticalSectionUnit1"/> <UnitName Value="CriticalSectionUnit1"/>
</Unit1> </Unit1>
</Units> </Units>

View File

@ -1,43 +1,43 @@
object Form1: TForm1 object Form1: TForm1
Caption = 'Form1'
ClientHeight = 169
ClientWidth = 312
PixelsPerInch = 75
HorzScrollBar.Page = 311
VertScrollBar.Page = 168
Left = 286 Left = 286
Height = 169 Height = 169
Top = 202 Top = 202
Width = 312 Width = 312
HorzScrollBar.Page = 311
VertScrollBar.Page = 168
ActiveControl = CountWithCritSecButton
Caption = 'Form1'
ClientHeight = 169
ClientWidth = 312
LCLVersion = '0.9.29'
object Label1: TLabel object Label1: TLabel
Caption = 'Label1'
Color = clNone
ParentColor = False
Left = 43 Left = 43
Height = 17 Height = 18
Top = 125 Top = 125
Width = 65 Width = 46
Caption = 'Label1'
ParentColor = False
end end
object CountWithCritSecButton: TButton object CountWithCritSecButton: TButton
Left = 40
Height = 37
Top = 24
Width = 196
AutoSize = True AutoSize = True
BorderSpacing.InnerBorder = 4 BorderSpacing.InnerBorder = 4
Caption = 'Count with critical section' Caption = 'Count with critical section'
OnClick = CountWithCritSecButtonClick OnClick = CountWithCritSecButtonClick
TabOrder = 0 TabOrder = 0
Left = 40
Height = 26
Top = 24
Width = 158
end end
object CountWithoutCritSecButton: TButton object CountWithoutCritSecButton: TButton
Left = 40
Height = 37
Top = 64
Width = 219
AutoSize = True AutoSize = True
BorderSpacing.InnerBorder = 4 BorderSpacing.InnerBorder = 4
Caption = 'Count without critical section' Caption = 'Count without critical section'
OnClick = CountWithoutCritSecButtonClick OnClick = CountWithoutCritSecButtonClick
TabOrder = 1 TabOrder = 1
Left = 40
Height = 26
Top = 64
Width = 175
end end
end end

View File

@ -1,16 +1,17 @@
{ Dies ist eine automatisch erzeugte Lazarus-Ressourcendatei } { This is an automatically generated lazarus resource file }
LazarusResources.Add('TForm1','FORMDATA',[ LazarusResources.Add('TForm1','FORMDATA',[
'TPF0'#6'TForm1'#5'Form1'#7'Caption'#6#5'Form1'#12'ClientHeight'#3#169#0#11'C' 'TPF0'#6'TForm1'#5'Form1'#4'Left'#3#30#1#6'Height'#3#169#0#3'Top'#3#202#0#5'W'
+'lientWidth'#3'8'#1#13'PixelsPerInch'#2'K'#18'HorzScrollBar.Page'#3'7'#1#18 +'idth'#3'8'#1#18'HorzScrollBar.Page'#3'7'#1#18'VertScrollBar.Page'#3#168#0#13
+'VertScrollBar.Page'#3#168#0#4'Left'#3#30#1#6'Height'#3#169#0#3'Top'#3#202#0 +'ActiveControl'#7#22'CountWithCritSecButton'#7'Caption'#6#5'Form1'#12'Client'
+#5'Width'#3'8'#1#0#6'TLabel'#6'Label1'#7'Caption'#6#6'Label1'#5'Color'#7#6'c' +'Height'#3#169#0#11'ClientWidth'#3'8'#1#10'LCLVersion'#6#6'0.9.29'#0#6'TLabe'
+'lNone'#11'ParentColor'#8#4'Left'#2'+'#6'Height'#2#17#3'Top'#2'}'#5'Width'#2 +'l'#6'Label1'#4'Left'#2'+'#6'Height'#2#18#3'Top'#2'}'#5'Width'#2'.'#7'Captio'
+'A'#0#0#7'TButton'#22'CountWithCritSecButton'#8'AutoSize'#9#25'BorderSpacing' +'n'#6#6'Label1'#11'ParentColor'#8#0#0#7'TButton'#22'CountWithCritSecButton'#4
+'.InnerBorder'#2#4#7'Caption'#6#27'Count with critical section'#7'OnClick'#7 +'Left'#2'('#6'Height'#2'%'#3'Top'#2#24#5'Width'#3#196#0#8'AutoSize'#9#25'Bor'
+#27'CountWithCritSecButtonClick'#8'TabOrder'#2#0#4'Left'#2'('#6'Height'#2#26 +'derSpacing.InnerBorder'#2#4#7'Caption'#6#27'Count with critical section'#7
+#3'Top'#2#24#5'Width'#3#158#0#0#0#7'TButton'#25'CountWithoutCritSecButton'#8 +'OnClick'#7#27'CountWithCritSecButtonClick'#8'TabOrder'#2#0#0#0#7'TButton'#25
+'AutoSize'#9#25'BorderSpacing.InnerBorder'#2#4#7'Caption'#6#30'Count without' +'CountWithoutCritSecButton'#4'Left'#2'('#6'Height'#2'%'#3'Top'#2'@'#5'Width'
+' critical section'#7'OnClick'#7#30'CountWithoutCritSecButtonClick'#8'TabOrd' +#3#219#0#8'AutoSize'#9#25'BorderSpacing.InnerBorder'#2#4#7'Caption'#6#30'Cou'
+'er'#2#1#4'Left'#2'('#6'Height'#2#26#3'Top'#2'@'#5'Width'#3#175#0#0#0#0 +'nt without critical section'#7'OnClick'#7#30'CountWithoutCritSecButtonClick'
+#8'TabOrder'#2#1#0#0#0
]); ]);

View File

@ -41,9 +41,11 @@ type
{ TMyThread } { TMyThread }
TMyThread = class(TThread) TMyThread = class(TThread)
private
FAFinished: boolean;
public public
procedure Execute; override; procedure Execute; override;
property Finished: boolean read FFinished write FFinished; property AFinished: boolean read FAFinished write FAFinished;
end; end;
{ TForm1 } { TForm1 }
@ -99,7 +101,7 @@ begin
repeat repeat
AllFinished:=true; AllFinished:=true;
for i:=Low(Threads) to High(Threads) do for i:=Low(Threads) to High(Threads) do
if not Threads[i].Finished then AllFinished:=false; if not Threads[i].AFinished then AllFinished:=false;
until AllFinished; until AllFinished;
// free the threads // free the threads
for i:=Low(Threads) to High(Threads) do for i:=Low(Threads) to High(Threads) do
@ -120,6 +122,7 @@ var
CurCounter: LongInt; CurCounter: LongInt;
j: Integer; j: Integer;
begin begin
FAFinished:=false;
// increment the counter many times // increment the counter many times
// Because the other threads are doing the same, it will frequently happen, // Because the other threads are doing the same, it will frequently happen,
// that 2 (or more) threads read the same number, increment it by one and // that 2 (or more) threads read the same number, increment it by one and
@ -137,7 +140,7 @@ begin
LeaveCriticalSection(Form1.CriticalSection); LeaveCriticalSection(Form1.CriticalSection);
end; end;
end; end;
Finished:=true; FAFinished:=true;
end; end;
initialization initialization

View File

@ -1,14 +1,18 @@
<?xml version="1.0"?> <?xml version="1.0"?>
<CONFIG> <CONFIG>
<ProjectOptions> <ProjectOptions>
<PathDelim Value="/"/> <Version Value="7"/>
<Version Value="6"/>
<General> <General>
<Flags>
<LRSInOutputDirectory Value="False"/>
</Flags>
<SessionStorage Value="InProjectDir"/> <SessionStorage Value="InProjectDir"/>
<MainUnit Value="0"/> <MainUnit Value="0"/>
<IconPath Value="./"/>
<TargetFileExt Value=""/> <TargetFileExt Value=""/>
</General> </General>
<VersionInfo>
<StringTable Comments="" CompanyName="" FileDescription="" FileVersion="0.0.0.0" InternalName="" LegalCopyright="" LegalTrademarks="" OriginalFilename="" ProductName="" ProductVersion="0.0.0.0"/>
</VersionInfo>
<PublishOptions> <PublishOptions>
<Version Value="2"/> <Version Value="2"/>
<IncludeFileFilter Value="*.(pas|pp|inc|lfm|lpr|lrs|lpi|lpk|sh|xml)"/> <IncludeFileFilter Value="*.(pas|pp|inc|lfm|lpr|lrs|lpi|lpk|sh|xml)"/>
@ -33,9 +37,9 @@
</Unit0> </Unit0>
<Unit1> <Unit1>
<Filename Value="mainunit.pas"/> <Filename Value="mainunit.pas"/>
<ComponentName Value="Form1"/>
<IsPartOfProject Value="True"/> <IsPartOfProject Value="True"/>
<ResourceFilename Value="mainunit.lrs"/> <ComponentName Value="Form1"/>
<ResourceBaseClass Value="Form"/>
<UnitName Value="MainUnit"/> <UnitName Value="MainUnit"/>
</Unit1> </Unit1>
</Units> </Units>

View File

@ -1,34 +1,36 @@
object Form1: TForm1 object Form1: TForm1
Caption = 'Form1'
ClientHeight = 142
ClientWidth = 303
OnCreate = FormCreate
PixelsPerInch = 75
HorzScrollBar.Page = 302
VertScrollBar.Page = 141
Left = 286 Left = 286
Height = 142 Height = 142
Top = 202 Top = 202
Width = 303 Width = 303
HorzScrollBar.Page = 302
VertScrollBar.Page = 141
ActiveControl = StartStopButton
Caption = 'Form1'
ClientHeight = 142
ClientWidth = 303
OnCreate = FormCreate
LCLVersion = '0.9.29'
object StartStopButton: TButton object StartStopButton: TButton
Left = 72
Height = 25
Top = 40
Width = 158
Anchors = [akTop] Anchors = [akTop]
BorderSpacing.InnerBorder = 4 BorderSpacing.InnerBorder = 4
Caption = 'Start' Caption = 'Start'
OnClick = StartStopButtonClick OnClick = StartStopButtonClick
TabOrder = 0 TabOrder = 0
Left = 72
Height = 25
Top = 40
Width = 158
end end
object ProgressBar1: TProgressBar object ProgressBar1: TProgressBar
Anchors = [akTop, akLeft, akRight]
Max = 1000
Smooth = True
Step = 1
Left = 32 Left = 32
Height = 20 Height = 20
Top = 88 Top = 88
Width = 244 Width = 244
Anchors = [akTop, akLeft, akRight]
Max = 1000
Smooth = True
Step = 1
TabOrder = 1
end end
end end

View File

@ -1,14 +1,14 @@
{ Dies ist eine automatisch erzeugte Lazarus-Ressourcendatei } { This is an automatically generated lazarus resource file }
LazarusResources.Add('TForm1','FORMDATA',[ LazarusResources.Add('TForm1','FORMDATA',[
'TPF0'#6'TForm1'#5'Form1'#7'Caption'#6#5'Form1'#12'ClientHeight'#3#142#0#11'C' 'TPF0'#6'TForm1'#5'Form1'#4'Left'#3#30#1#6'Height'#3#142#0#3'Top'#3#202#0#5'W'
+'lientWidth'#3'/'#1#8'OnCreate'#7#10'FormCreate'#13'PixelsPerInch'#2'K'#18'H' +'idth'#3'/'#1#18'HorzScrollBar.Page'#3'.'#1#18'VertScrollBar.Page'#3#141#0#13
+'orzScrollBar.Page'#3'.'#1#18'VertScrollBar.Page'#3#141#0#4'Left'#3#30#1#6'H' +'ActiveControl'#7#15'StartStopButton'#7'Caption'#6#5'Form1'#12'ClientHeight'
+'eight'#3#142#0#3'Top'#3#202#0#5'Width'#3'/'#1#0#7'TButton'#15'StartStopButt' +#3#142#0#11'ClientWidth'#3'/'#1#8'OnCreate'#7#10'FormCreate'#10'LCLVersion'#6
+'on'#7'Anchors'#11#5'akTop'#0#25'BorderSpacing.InnerBorder'#2#4#7'Caption'#6 +#6'0.9.29'#0#7'TButton'#15'StartStopButton'#4'Left'#2'H'#6'Height'#2#25#3'To'
+#5'Start'#7'OnClick'#7#20'StartStopButtonClick'#8'TabOrder'#2#0#4'Left'#2'H' +'p'#2'('#5'Width'#3#158#0#7'Anchors'#11#5'akTop'#0#25'BorderSpacing.InnerBor'
+#6'Height'#2#25#3'Top'#2'('#5'Width'#3#158#0#0#0#12'TProgressBar'#12'Progres' +'der'#2#4#7'Caption'#6#5'Start'#7'OnClick'#7#20'StartStopButtonClick'#8'TabO'
+'sBar1'#7'Anchors'#11#5'akTop'#6'akLeft'#7'akRight'#0#3'Max'#3#232#3#6'Smoot' +'rder'#2#0#0#0#12'TProgressBar'#12'ProgressBar1'#4'Left'#2' '#6'Height'#2#20
+'h'#9#4'Step'#2#1#4'Left'#2' '#6'Height'#2#20#3'Top'#2'X'#5'Width'#3#244#0#0 +#3'Top'#2'X'#5'Width'#3#244#0#7'Anchors'#11#5'akTop'#6'akLeft'#7'akRight'#0#3
+#0#0 +'Max'#3#232#3#6'Smooth'#9#4'Step'#2#1#8'TabOrder'#2#1#0#0#0
]); ]);

View File

@ -29,7 +29,7 @@ interface
uses uses
Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, Buttons, Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, Buttons,
ComCtrls, FileUtil; ComCtrls, FileUtil, StdCtrls;
type type
@ -85,7 +85,8 @@ begin
fs:=TFileStream.Create(UTF8ToSys(Filename),fmOpenRead); fs:=TFileStream.Create(UTF8ToSys(Filename),fmOpenRead);
try try
SetLength(Buffer,10); SetLength(Buffer,10);
while true do begin while true do
begin
// process all user events, like clicking on the button // process all user events, like clicking on the button
Application.ProcessMessages; Application.ProcessMessages;
@ -96,7 +97,8 @@ begin
if Count=0 then break; if Count=0 then break;
// process ... // process ...
for i:=1 to Count do begin for i:=1 to Count do
begin
Last:=Now; Last:=Now;
repeat repeat
until Now-Last>(double(1)/fs.Size)/10000; until Now-Last>(double(1)/fs.Size)/10000;

View File

@ -1,14 +1,18 @@
<?xml version="1.0"?> <?xml version="1.0"?>
<CONFIG> <CONFIG>
<ProjectOptions> <ProjectOptions>
<PathDelim Value="/"/> <Version Value="7"/>
<Version Value="6"/>
<General> <General>
<Flags>
<LRSInOutputDirectory Value="False"/>
</Flags>
<SessionStorage Value="InProjectDir"/> <SessionStorage Value="InProjectDir"/>
<MainUnit Value="0"/> <MainUnit Value="0"/>
<IconPath Value="./"/>
<TargetFileExt Value=""/> <TargetFileExt Value=""/>
</General> </General>
<VersionInfo>
<StringTable Comments="" CompanyName="" FileDescription="" FileVersion="0.0.0.0" InternalName="" LegalCopyright="" LegalTrademarks="" OriginalFilename="" ProductName="" ProductVersion="0.0.0.0"/>
</VersionInfo>
<PublishOptions> <PublishOptions>
<Version Value="2"/> <Version Value="2"/>
<IgnoreBinaries Value="False"/> <IgnoreBinaries Value="False"/>
@ -34,9 +38,9 @@
</Unit0> </Unit0>
<Unit1> <Unit1>
<Filename Value="processmessagesunit1.pas"/> <Filename Value="processmessagesunit1.pas"/>
<ComponentName Value="Form1"/>
<IsPartOfProject Value="True"/> <IsPartOfProject Value="True"/>
<ResourceFilename Value="processmessagesunit1.lrs"/> <ComponentName Value="Form1"/>
<ResourceBaseClass Value="Form"/>
<UnitName Value="ProcessMessagesUnit1"/> <UnitName Value="ProcessMessagesUnit1"/>
</Unit1> </Unit1>
</Units> </Units>

View File

@ -1,14 +1,18 @@
<?xml version="1.0"?> <?xml version="1.0"?>
<CONFIG> <CONFIG>
<ProjectOptions> <ProjectOptions>
<PathDelim Value="/"/> <Version Value="7"/>
<Version Value="6"/>
<General> <General>
<Flags>
<LRSInOutputDirectory Value="False"/>
</Flags>
<SessionStorage Value="InProjectDir"/> <SessionStorage Value="InProjectDir"/>
<MainUnit Value="0"/> <MainUnit Value="0"/>
<IconPath Value="./"/>
<TargetFileExt Value=""/> <TargetFileExt Value=""/>
</General> </General>
<VersionInfo>
<StringTable Comments="" CompanyName="" FileDescription="" FileVersion="0.0.0.0" InternalName="" LegalCopyright="" LegalTrademarks="" OriginalFilename="" ProductName="" ProductVersion="0.0.0.0"/>
</VersionInfo>
<PublishOptions> <PublishOptions>
<Version Value="2"/> <Version Value="2"/>
<IncludeFileFilter Value="*.(pas|pp|inc|lfm|lpr|lrs|lpi|lpk|sh|xml)"/> <IncludeFileFilter Value="*.(pas|pp|inc|lfm|lpr|lrs|lpi|lpk|sh|xml)"/>
@ -33,9 +37,9 @@
</Unit0> </Unit0>
<Unit1> <Unit1>
<Filename Value="waitforunit1.pas"/> <Filename Value="waitforunit1.pas"/>
<ComponentName Value="Form1"/>
<IsPartOfProject Value="True"/> <IsPartOfProject Value="True"/>
<ResourceFilename Value="waitforunit1.lrs"/> <ComponentName Value="Form1"/>
<ResourceBaseClass Value="Form"/>
<UnitName Value="WaitForUnit1"/> <UnitName Value="WaitForUnit1"/>
</Unit1> </Unit1>
</Units> </Units>

View File

@ -11,6 +11,7 @@ object Form1: TForm1
ClientWidth = 394 ClientWidth = 394
OnCreate = FormCreate OnCreate = FormCreate
OnDestroy = FormDestroy OnDestroy = FormDestroy
LCLVersion = '0.9.29'
object Button1: TButton object Button1: TButton
Left = 32 Left = 32
Height = 25 Height = 25
@ -22,9 +23,11 @@ object Form1: TForm1
TabOrder = 0 TabOrder = 0
end end
object Memo1: TMemo object Memo1: TMemo
Left = 0
Height = 303 Height = 303
Top = 56 Top = 56
Width = 394 Width = 394
Anchors = [akTop, akLeft, akRight, akBottom]
ScrollBars = ssAutoBoth ScrollBars = ssAutoBoth
TabOrder = 1 TabOrder = 1
end end

View File

@ -1,13 +1,14 @@
{ Dies ist eine automatisch erzeugte Lazarus-Ressourcendatei } { This is an automatically generated lazarus resource file }
LazarusResources.Add('TForm1','FORMDATA',[ LazarusResources.Add('TForm1','FORMDATA',[
'TPF0'#6'TForm1'#5'Form1'#4'Left'#3#30#1#6'Height'#3'g'#1#3'Top'#3#202#0#5'Wi' 'TPF0'#6'TForm1'#5'Form1'#4'Left'#3#30#1#6'Height'#3'g'#1#3'Top'#3#202#0#5'Wi'
+'dth'#3#138#1#18'HorzScrollBar.Page'#3#137#1#18'VertScrollBar.Page'#3'f'#1#13 +'dth'#3#138#1#18'HorzScrollBar.Page'#3#137#1#18'VertScrollBar.Page'#3'f'#1#13
+'ActiveControl'#7#7'Button1'#7'Caption'#6#5'Form1'#12'ClientHeight'#3'g'#1#11 +'ActiveControl'#7#7'Button1'#7'Caption'#6#5'Form1'#12'ClientHeight'#3'g'#1#11
+'ClientWidth'#3#138#1#8'OnCreate'#7#10'FormCreate'#9'OnDestroy'#7#11'FormDes' +'ClientWidth'#3#138#1#8'OnCreate'#7#10'FormCreate'#9'OnDestroy'#7#11'FormDes'
+'troy'#0#7'TButton'#7'Button1'#4'Left'#2' '#6'Height'#2#25#3'Top'#2#16#5'Wid' +'troy'#10'LCLVersion'#6#6'0.9.29'#0#7'TButton'#7'Button1'#4'Left'#2' '#6'Hei'
+'th'#2'K'#25'BorderSpacing.InnerBorder'#2#4#7'Caption'#6#5'Start'#7'OnClick' +'ght'#2#25#3'Top'#2#16#5'Width'#2'K'#25'BorderSpacing.InnerBorder'#2#4#7'Cap'
+#7#12'Button1Click'#8'TabOrder'#2#0#0#0#5'TMemo'#5'Memo1'#6'Height'#3'/'#1#3 +'tion'#6#5'Start'#7'OnClick'#7#12'Button1Click'#8'TabOrder'#2#0#0#0#5'TMemo'
+'Top'#2'8'#5'Width'#3#138#1#10'ScrollBars'#7#10'ssAutoBoth'#8'TabOrder'#2#1#0 +#5'Memo1'#4'Left'#2#0#6'Height'#3'/'#1#3'Top'#2'8'#5'Width'#3#138#1#7'Anchor'
+#0#0 +'s'#11#5'akTop'#6'akLeft'#7'akRight'#8'akBottom'#0#10'ScrollBars'#7#10'ssAut'
+'oBoth'#8'TabOrder'#2#1#0#0#0
]); ]);