Delphi thread synchronization critical section

Source: Internet
Author: User

The simplest thing about dephi thread synchronization is the critical section.

The critical section indicates the code that can only be executed by one thread at a time.

Before using the critical section, you must use the initializecriticalsection () process to initialize it.
The statement is as follows:

Procedure initializecriticalsection (VAR lpcriticalsection); stdcall;

The lpcriticalsection parameter is a record of the trtlcriticalsection type and is a variable parameter. For trtlcriticalsection
It doesn't matter how it is defined, because it is seldom necessary to view the specific content in this record. It only needs to be passed in the lpcriticalsection
When an uninitialized record is passed, the initializecriticalsection () process will fill the record.

After the record is filled, we can create a critical section. In this case, we need to use e n t e r c r I t I c a l e c t I o n () and l e a v e c r I t I c a l s e c t I o n () to encapsulate code blocks.

When you do not need t rt l c r I c a l s e c t I o n records, the procedure of calling d e l e t e c r I t I c a l s e c t I o n () should be called. The following is its statement:
Procedure deletecriticalsection (VAR lpcriticalsection: trt l c r I I c a l s e c t I o n); S T D C A L;

Column:

unit CriticaSection;interfaceuses  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;type  TForm2 = class(TForm)    btn1: TButton;    lstbox1: TListBox;    procedure btn1Click(Sender: TObject);  public    procedure ThreadsDone(Sender : TObject);  end;  TFooThread=class(TThread)    protected      procedure Execute;override;  end;var  Form2: TForm2;implementation{$R *.dfm}const  MAXSIZE=128;var  NextNumber : Integer=0;  DoneFlags : Integer=0;  GlobalArray : array[1..MAXSIZE] of Integer;  CS : TRTLCriticalSection;function GetNextNumber:Integer;begin  Result:=NextNumber;  Inc(nextnumber);end;{ TForm2 }procedure TForm2.btn1Click(Sender: TObject);begin // InitializeCriticalSection(CS);  TFooThread.Create(False);  TFooThread.Create(False);end;procedure TForm2.ThreadsDone(Sender: TObject);var  i:Integer;begin  Inc(DoneFlags);  if DoneFlags=2 then  for I := 1 to MAXSIZE do    lstbox1.Items.Add(IntToStr(GlobalArray[i]));  DeleteCriticalSection(CS);end;{ TFooThread }procedure TFooThread.Execute;var  i:Integer;begin  EnterCriticalSection(cs);  OnTerminate:=Form2.ThreadsDone;  for I := 1 to MAXSIZE do  begin    GlobalArray[i]:=GetNextNumber;  end;  LeaveCriticalSection(CS);end;end.

In this way, it can be thread synchronization without conflict.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.