Reprinted from: In Case of blog
See a program first
Unit unit1;interfaceuses Windows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms, Dialogs, Stdctrls;type Tform1=class (tform) Listbox1:tlistbox; Button1:tbutton; Procedure Formcreate (sender:tobject); Procedure Button1Click (sender:tobject); End;var form1:tform1;implementation{$R *.dfm}function mythreadfun (p:pointer): DWORD; Stdcall;var I:integer ; Begin for i:=0 to Form1.ListBox1.Items.Add (IntToStr (i)); Notice how the Tlistbox is used (and the simple use of a component is learned) result:= 0;end;procedure Tform1.button1click (sender:tobject); var ID: Dword;begin CreateThread (nil, 0, @MyThreadFun, nil, 0, ID); CreateThread (nil, 0, @MyThreadFun, nil, 0, ID); CreateThread (nil, 0, @MyThreadFun, nil, 0, ID); End;procedure tform1.formcreate (sender:tobject); Begin Listbox1.align:= Alleft;end;end.
The form file is as follows
Object Form1:tform1 Left = 0 Top = 0 Caption = ' Form1 ' clientheight = 154 clientwidth = 214 Color = Clbtnface font.charset = default_charset font.color = clwindowtext font.height = -11 font.name = ' Tahoma ' font.style = [] oldcreateorder = False OnCreate = formcreate pixelsperinch = TextHeight = Object Listbox1:tlistbox Left = 9 Top = 9 Width = 121 Height = Itemheigh t = taborder = 0 End Object Button1:tbutton Left = 131 Top = Width = Height = Caption = ' Button1 ' taborder = 1 OnClick = Button1Click endend
In this program, three threads are created almost at the same time, writing data to the ListBox1 in the form, and the final result is this.
Can you let them not fight, one to finish the other again? This is going to use the multi-threaded synchronization technology
As I said earlier, the simplest means of synchronization is the "Critical zone"
First say this "Synchronization (Synchronize)", first of all, this name is not good, we seem to need is "asynchronous", in fact, "async" is not accurate ....
What is the name of it, the purpose of which is to ensure that there is no conflict, order, all occur
"critical section (criticalsection": when a piece of code into a critical section , the thread executes to the critical section is independent, so that the other to execute the code of the line enters upgradeable and so on ; this front uses the same lock and unlock. ; Use the following format
var cs:trtlcriticalsection; Declares a trtlcriticalsection struct type variable. It should be the global initializecriticalsection (CS); Initialize EnterCriticalSection (CS); Start, it's my turn, other threads go away leavecriticalsection (CS); End, I executed well, other threads can come deletecriticalsection (CS); Delete, note cannot be deleted prematurely//can also use tryentercriticalsection instead of entercriticalsection
Use the upper critical section, rewrite the above code, and run the following
The code files are as follows
Unit unit1;interfaceuses Windows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms, Dialogs, Stdctrls;ty PE tform1=class (tform) Listbox1:tlistbox; Button:tbutton; Procedure Formcreate (Sender:tobject); Procedure Formdestory (Sender:tobject); Procedure Button1Click (Sender:tobject); End;var form1:tform1;implementation{$R *.dfm}var cs:trtlcriticalsection;function MyThreadFun (p:pointer): DWORD; Stdcall;var i:integer;begin entercriticalsection (CS); For i:=0 to Form1.ListBox1.Items.Add (IntToStr (i)); LeaveCriticalSection (CS); result:= 0;end;procedure Tform1.button1click (sender:tobject); var id:dword;begin createthread (nil, 0, @MyThreadFun, Nil, 0, ID); CreateThread (nil, 0, @MyThreadFun, nil, 0, ID); CreateThread (nil, 0, @MyThreadFun, nil, 0, ID); End;procedure tform1.formcreate (sender:tobject); Begin listbox1.align:= Alleft; InitializeCriticalSection (CS); End;procedure tform1.foRmdestory (sender:tobject); begin DeleteCriticalSection (CS); end;end.
Delphi in the SYNCOBJS unit to encapsulate a tcriticalsection class, Yongda almost, the code is as follows
Unit unit1;interfaceuses Windows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms, Dialogs, Stdctrls;ty PE tform1=class (tform) Listbox1:tlistbox; Button1:tbutton; Procedure Formcreate (Sender:tobject); Procedure Formdestory (Sender:tobject); Procedure Button1Click (Sender:tobject); End;var form1:tform1;implementation{$R *.dfm}uses syncobjs;var cs:tcriticalsection;function MyThreadFun (p:pointer ): DWORD; Stdcall;var I:integer;begin CS. Enter; For i:=0 to Form1.ListBox1.Item.Add (IntToStr (i)); Cs. Leave; result:= 0;end;procedure Tform1.button1click (sender:tobject); var createthread (nil, 0, @MyThreadFun, nil, 0, ID); CreateThread (nil, 0, @MyThreadFun, nil, 0, ID); CreateThread (nil, 0, @MyThreadFun, nil, 0, ID); End;procedure tform1.formcreate (sender:tobject); Begin listbox1.align:= Alleft; Cs:= Tcriticalsection.create;end; Procedure tform1.formdestory (sender:tobject); Begin cs.frEe;end;end.
Delphi multithreaded Programming (8)--Multithreading synchronization CriticalSection (critical section)