Threads in Delphi - (1)

zhaozj2021-02-08  206

Thread class in Delphi

Raptor [Mental Studio]

Http://eental.mentsu.com

(One)

There is a thread TTHREAD in Delphi to implement multi-threaded programming. This most Delphi book is said, but it is basically a simple introduction to several members of the TTHRead class, then explain Execute's Implementation and Synchronize will be finished. However, this is not all of the multi-threaded programming, and I have written this article to make a supplement.

The thread is essentially the code in the process in the process. There is at least one thread, the so-called main thread. Also there can be multiple sub-threads. When more than one thread is used in a process, the so-called "multi-thread".

So how is this so-called "one code" defined? In fact, it is a function or process (for Delphi).

If you create a thread with a Windows API, it is implemented by a API function called CreateThread, which is defined as:

Handle CreateThread

LPSecurity_attributes LPTHREADATIADATTRIBUTES,

DWORD DWSTACKSIZE,

LPTHREAD_START_ROUTINE LPSTARTDRESS,

LPVOID LPPARETER,

DWORD DWCREATIONFLAGS,

LPDWORD LPTHREADID

);

The parameters of each parameter are said, respectively: thread properties (for threading security attribute settings in NT, invalid at 9X), stack size, start addresses, parameters, creation flags (for setting threads) State in creation), thread ID, and finally returns threaded handle. The start address is the entry of the thread function until the thread function ends, the thread is over.

The implementation process of the entire thread is as follows:

Because the CreateThread parameters are many, and is the Windows API, a universal thread function is provided in C Runtime Library (theoretically use in an OS that supports threads):

Unsigned long _beginthread (void (_USERENTRY * __ START) (void *), unsigned __stksize, void * __ arg);

Delphi also provides a similar function for the same function:

Function Beginthread (SecurityAttributes: Pointer; Stacksize: longword; threadfunc: tthreadfunc; parameter: Pointer; Creger; Var ThreadId: longword: integer;

The functions of these three functions are basically the same, and they are all putting the code in the thread function in a separate thread. The maximum difference between the thread function and the general function is that the thread function starts, the three thread start functions return, the main thread continues to execute down, and the thread function is executed in a separate thread, how long it is to do, what When returning, the main thread is not known.

Under normal circumstances, the thread is returned, the thread is terminated. But there are other ways:

Windows API:

Void ExitThread (DWORD DWEXITCODE);

C Runtime Library:

Void_endthread (void);

Delphi Runtime Library: Procedure Endthread (EXITCODE: Integer);

In order to record some necessary thread data (status / attributes, etc.), OS will create an internal object for threads, such as the Handle in Windows is the Handle of this internal Object, so you should release this Object when the thread is over.

Although it is easy to make multi-thread programming with API or RTL (Runtime Library), it is still necessary to make more detail processing, and this Delphi is a better package in the Classes unit, which is VCL thread class: TTHREAD

It is also very simple to use this class, and most of the Delphi books say that the basic usage is: first derive a self-a thread class from TTHRead (because TTHREAD is an abstract class, can not generate an instance), then the Override abstract method: EXECUTE This is the thread function, that is, the code section executed in the thread), if you need to use a visual VCL object, you will also need to pass through the SYNCHRONIZE process. For details, details are not described here, please refer to the relevant books.

This article will discuss how the TTHREAD class packages threads, which is to study the implementation of the TTHREAD class. Because it just truly understands it, it is better to use it.

The following is a statement of the TTHREAD class in Delphi7 (this article only discusses the implementation under the Windows platform, so removes all the code about the Linux platform part):

TTHREAD = Class

Private

Fhandle: Thandle;

FTHREADID: THANDLE;

Fcreatesuspended: boolean;

Fterminated: boolean;

Fsuspended: boolean;

Ffreeonterminate: boolean;

FFINIShed: Boolean;

FREGER DIRUE: Integer;

Fonterminate: tnotifyevent;

Fsynchronize: TsynchronizeRecord;

FfaTalexception: TOBJECT;

Procedure callonterminate;

Class Procedure Synchronize (AsyncRec: psynchronizerecord); overload;

Function getPriority: tthreadpriority;

Procedure SetPriority (Value: TthreadPriority);

Procedure setsuspended (value: boolean);

protected

Procedure CheckthReaderror (Errcode: Integer); OVERLOAD;

Procedure Checkthreaderror (Success: Boolean); OVERLOAD;

PROCEDURE DOTERMINATE; Virtual;

.

Procedure Synchronize (Method: TthreadMethod); OVERLOAD;

Property ReturnValue: Integer Read FreturnValue Write FreTurnValue;

Property Terminated: Boolean Read Fterminated;

public

Constructor create (createSuspended: Boolean); Destructor Destroy; Override;

Procedure afterconstruction; Override;

PROCEDURE RESUME;

Procedure supend;

Procedure terminate;

Function WAITFOR: Longword;

Class Procedure Synchronize (Athread: Tthread; "OVERLOAD;

Class Procedure Staticsynchronize (ATHREAD: TTHREAD; AMETHOD: TTHREADMETHOD);

Property Fatalexception: TOBJECT Read FfataLexception;

Property FreeOnterminate: Boolean Read FFreeonterminate Write ffreeOnterminate;

Property Handle: Thandle Read Fhandle;

Property priority: tthreadpriority read priority write setpriority;

Property Suspended: Boolean Read Fsuspended Write Setsuspend;

Property Threadid: Thandle Read FthreadId;

Property Onterminate: TNOTIFYEVENT Read Fonterminate Write Fonterminate;

END;

TTHREAD class is a relatively simple class in the RTL of Delphi. There are not many class members. Class properties are simply understood. This article will only be more important to several important class members and unique events: onterminate.

(to be continued)

转载请注明原文地址:https://www.9cbs.com/read-2666.html

New Post(0)