In the process of software development, multi-threaded development applications have an extremely important position. The use of multi-threaded can make the Software System
It can operate in parallel and improve its running efficiency. As a required course for software developers, multi-thread proficiency
The application can improve the performance of the software system.
The following uses the window API and the tthread class encapsulated by Delphi to create threads respectively, and then runs them in a normal way.
A time-consuming process compares the advantages of using threads.
1. Create Windows API functions directly
The main function is the createthread function. Its function formula is as follows:
Handle createthread (
// Thread Security Attribute. Nil is used by default.
Lpsecurity_attributes lpthreadattributes,
// The stack size allocated by the thread. The default value is 0. The system automatically allocates the stack size.
DWORD dwstacksize,
// The thread function entry address, which must be a global method without Parameters
Lpthread_start_routine lpstartaddress,
// Parameter value passed to the thread
Lpvoid lpparameter,
// ID of the Creation thread, such as create_suincluded (suspended)
DWORD dwcreationflags,
// The returned thread ID Identifier
Lpdword lpthreadid
);
If the thread is successfully created, the return value is its handle value.
Create a new form in the Delphi project and write down the following thread functions to call
Procedure apithread1;
VaR I, CID: integer;
Begin
With form2 do
Begin
For I: = 1 to IMAX do
Begin
Entercriticalsection (RTL );
CID: = getcurrentthreadid; // gets the ID of the currently executed thread
Memo1.lines. Add ('thread id' + inttostr (CID) + ':' + inttostr (I ));
Leavecriticalsection (RTL );
End;
End;
End;
To prevent errors caused by simultaneous access (read/write) to the same VCL resource by multiple threads, the critical variable RTL is used,
Therefore, we need to declare a local critical variable.
VaR
RTL: trtlcriticalsection;
It serves as a protocol lock. For example, when the apithread1 function is createthread
(Nil, 0, @ apithread1, nil, 0, hid)
When different threads are generated multiple times, a write operation is performed on memo1, and one of the threads is
In the process, use the RTL critical variable to tell other threads to be accessed: "I am using it. Please wait. "
Note:
Entering the critical section entercriticalsection (RTL) and leaving the critical section leavecriticalsection (RTL) is
Therefore, before using the critical zone variables, you must initialize them,
Initializecriticalsection (RTL), after which deletecriticalsection (RTL) is released );
2. Create a tthread class encapsulated by Delphi
It inherits the tthread class and executes the operation code you want in execute, the core method of the tthread class.
In fact, we can see from the source code of the tthread class that tthread also calls the createthread function to create a line.
It only encapsulates/simplifies the application details of some threads and also incorporates some security considerations.
The caller provides convenience for calling.
Methods, such as suspending suspend, awakening resume, and terminating terminate.
Data Description: This is only a simple example of a thread call.
Declare the inheritance class first
Type
Tmythread = Class (tthread)
Private
Fflag: integer;
Procedure accessvcl;
Protected
Procedure execute; override;
Public
Constructor create (aflag: integer );
End;
Write the Method Content in implementation
Constructor tmythread. Create (aflag: integer );
Begin
Inherited create (suincluded );
Freeonterminate: = true;
Fflag: = aflag;
End;
Procedure tmythread. Execute;
Begin
Inherited;
Accessvcl;
// Synchronize (accessvcl );
End;
Procedure tmythread. accessvcl;
VaR I: integer;
Begin
With form2 do
Begin
For I: = 1 to IMAX do
Memo1.lines. Add ('My thread' + inttostr (fflag) + ':' + inttostr (I ));
End;
End;
Call execution thread: tmythread. Create (10 );
3. Write a common for loop to execute a time-consuming operation method.
For I: = 1 to IMAX do
Begin
CID: = getcurrentthreadid;
Memo1.lines. Add ('thread id' + inttostr (CID) + ':' + inttostr (I ));
End;
Call the API Thread method, Delphi tthread thread method, and common operation method on the form respectively, and then compare
The execution effect can reflect the advantages of thread methods and non-threads in real time.
The source code is as follows:
View plaincopy to clipboardprint?
Unit unit2;
Interface
Uses
Windows, messages, sysutils, variants, classes, graphics, controls, forms,
Dialogs, stdctrls;
Type
Tform2 = Class (tform)
Memo1: tmemo;
Btnthread1: tbutton;
Btnthread2: tbutton;
Btnnothread: tbutton;
Button1: tbutton;
Btnclear: tbutton;
Btnmythread: tbutton;
Button2: tbutton;
Procedure btnthread1click (Sender: tobject );
Procedure btnthread2click (Sender: tobject );
Procedure btnnothreadclick (Sender: tobject );
Procedure button1click (Sender: tobject );
Procedure btnclearclick (Sender: tobject );
Procedure btnmythreadclick (Sender: tobject );
Procedure button2click (Sender: tobject );
Private
{Private Declarations}
Public
{Public declarations}
End;
{Inherited Thread class}
Type
Tmythread = Class (tthread)
Private
Fflag: integer;
Procedure accessvcl;
Protected
Procedure execute; override;
Public
Constructor create (aflag: integer );
End;
Const
IMAX = 2000;
VaR
Form2: tform2;
RTL: trtlcriticalsection;
Thd1, thd2: Cardinal;
Implementation
{$ R *. DFM}
// API Thread method
Procedure apithread1;
VaR I, CID: integer;
Begin
With form2 do
Begin
For I: = 1 to IMAX do
Begin
Entercriticalsection (RTL );
CID: = getcurrentthreadid;
Memo1.lines. Add ('thread id' + inttostr (CID) + ':' + inttostr (I ));
Leavecriticalsection (RTL );
End;
End;
End;
Constructor tmythread. Create (aflag: integer );
Begin
Inherited create (suincluded );
Freeonterminate: = true;
Fflag: = aflag;
End;
Procedure tmythread. Execute;
Begin
Inherited;
Accessvcl;
// Synchronize (accessvcl );
End;
Procedure tmythread. accessvcl;
VaR I: integer;
Begin
With form2 do
Begin
For I: = 1 to IMAX do
Memo1.lines. Add ('My thread' + inttostr (fflag) + ':' + inttostr (I ));
End;
End;
Procedure tform2.btnthread1click (Sender: tobject );
VaR hid: thandle;
Begin
// API Thread call 1
Thd1: = createthread (nil, 0, @ apithread1, nil, 0, hid );
End;
Procedure tform2.btnthread2click (Sender: tobject );
VaR hid: thandle;
Begin
// API Thread call 2
Thd2: = createthread (nil, 0, @ apithread1, nil, 0, hid );
End;
Procedure tform2.btnnothreadclick (Sender: tobject );
VaR I, CID: integer;
Begin
// The normal method executes the For Loop
For I: = 1 to IMAX do
Begin
CID: = getcurrentthreadid;
Memo1.lines. Add ('thread id' + inttostr (CID) + ':' + inttostr (I ));
End;
End;
Procedure tform2.button1click (Sender: tobject );
Begin
Showmessage ('execute other operations ');
End;
Procedure tform2.btnclearclick (Sender: tobject );
Begin
Memo1.lines. Clear;
End;
Procedure tform2.btnmythreadclick (Sender: tobject );
Begin
// Call method of the Delphi tthread inherited class
Tmythread. Create (11 );
End;
Procedure tform2.button2click (Sender: tobject );
VaR Ext: thandle;
Begin
Getexitcodethread (thd1, ext );
Terminatethread (thd1, ext );
// Leavecriticalsection (RTL );
End;
Initialization
Initializecriticalsection (RTL );
Finalization
Deletecriticalsection (RTL );
End.