Delphi Multi-Threaded Onterminate attribute (attach an example of thread synchronization in critical section)

Source: Internet
Author: User

  First look at the TThread source code about Onterminate:

Public    ....    Property Onterminate:tnotifyevent read Fonterminate write fonterminate;    ... end;

  See the explanation of Onterminate in the Help Manual of Delphi:

Occurs after the thread's Execute method has returned and before, the thread is destroyed.

Property onterminate:tnotifyevent;

Description

Write a Onterminate event handler to execute code after the thread finishes executing. The Onterminate event handler is called in the context of the main thread, which means CLX methods and properties can be C Alled freely.

The general explanation for translating into Chinese is that if this property of a thread is assigned to a method, then this method will execute when the thread's Execute method executes and exits, but the thread has not yet been freed.

  A practical example:

Unit unit1;interfaceuses Windows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms, Dialogs, Stdctrls;ty    PE TForm1 = Class (Tform) Btn1:tbutton;  Procedure Btn1click (Sender:tobject);  Private procedure Threaddone (Sender:tobject);  Public {public declarations} end; Ttestthread = Class (TThread) protected procedure Execute;  Override  End;var form1:tform1;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  Procedure Ttestthread.execute;  var I:integer;    Begin onterminate:= Form1.threaddone;    Here you set the Threaddone method of the Onterminate property to Form1, which indicates that the Threaddone method of Form1 is executed immediately after the thread executes execute and before it is released.    EnterCriticalSection (CS);      For i:= 1 to MaxSize do begin globalarray[i]:= Getnextnumber;    Sleep (5);    End LeaveCriticalSection (CS);  End  Procedure Tform1.threaddone (Sender:tobject);  var I:integer;    Begin Inc (DONEFLAGS); If Doneflags = 2 then the begin for i:= 1 to MaxSize do lst1.      Items.Add (IntToStr (globalarray[i));    DeleteCriticalSection (CS);  End  End;procedure Tform1.btn1click (sender:tobject); begin InitializeCriticalSection (CS);  Ttestthread.create (False); Ttestthread.create (False); end;end.

  explain onterminate: Here you set the Threaddone method with the Onterminate property Form1, which means that after the thread executes execute, it is not released before The Threaddone method to execute the FORM1 immediately thereafter.

  The synchronization of Threads is described here: the thread is synchronized using a critical section, which opens two threads after the button is clicked.

Each thread operates on a global array globalarray, using a critical section, (see TForm1 's Btn1click method), although it is open two threads in a row, but because the critical section is used, and is in the thread's execute before the operation to enter the critical section, So when a thread runs (that is, executes the Execute method), the first thread enters the critical section to globalarray the global array, and then the other thread waits until the first thread finishes the operation and leaves the critical section ( The value of the Globalarray array is now from 0 to 127). The second thread starts to enter the critical section and begins operation on Globalarray, and the value in the last Globalarray array is from 128 to 255.

By using a critical section, you can guarantee that multiple threads cannot operate on a resource at the same time, and if the thread is not synchronized using a critical section, it could end up with thread 1, Thread 2 operates randomly on a contiguous array of arrays (because the operating system allocates the time slices of the thread randomly) (and nextnumber This global variable is randomly contiguous by two threads, which can cause conflicts), and the final result is that when two threads are executed, The values in the Globalarray array do not have any regularity, so it is not possible to control the thread according to the rules at this time, the program developed is not controlled by the developer, obviously not conducive to the stability of the program.

               So when multiple threads are going to operate on the same resource, the thread must be synchronized to ensure the thread is controllable.

Delphi Multi-threaded Onterminate attribute (attach an example of thread synchronization in critical section)

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.