The basic concept of multithreading and Delphi Threading Object TThread Introduction

Source: Internet
Author: User
Tags semaphore thread class

The basic concept of multithreading and Delphi Threading Object TThread IntroductionXiaoruWIN 98/NT/2000/XP is a multitasking operating system, that is: a process can be divided into multiple threads, each thread takes up CPU uptime and resources, or divides the CPU time into slices, each slice to different threads, so that each thread turns "hang" and "wake" Because the time slice is very small, the feeling that gives the person is running at the same time. Multithreading brings the following benefits:1) Avoid bottlenecks, 2) parallel operation, 3) improve efficiency;
Two concepts of multi-threading:1) Process: Also called a task, the program loads the memory and allocates resources, called "a process". Note: The process itself does not necessarily need to be executing. The process consists of the following parts: a> a private address space, which is a set of virtual memory address space that the process can use, the related code of the B> program, the data source, c> system resources, such as operating system synchronization object, D> contains at least one thread (main thread);
2) Thread: is the execution unit of the process (the thread itself does not include the program code, the actual code is the process), is the operating system allocates CPU time of the basic entity, each process includes at least one thread, called the main threads. If a process has multiple threads, it can share the resources of the same process and execute concurrently. The popular point is the code (a function or procedure) that runs concurrently in a process.
threads consist mainly of the following two parts:a> data structure; b>cpu registers and stacks;
The thread function runs, the START function returns, the main thread continues to execute down, and the thread function executes in a separate thread, how long it executes, when it returns, and whether or not the main thread is known.
One, Delphi thread object---tthread
Although Windows provides many API functions for multithreaded design, it is extremely inconvenient to use API functions directly, and error-prone when used improperly. In order to solve this problem, Borland company first introduced a kind of tthread object to solve the difficulty of multithreading design, and simplified the processing of multithreading problem.

First, the main method of TThread object
To construct a thread:
Constructor Create (Createsuspended:boolean)
Createsuspended=true constructs but does not wake, false constructs the same time wakes.
Suspend thread: Suspend: (Add a number of threads to hang)
Wake-up Thread: Resume: (Note: Note that this property is the number of times a thread is suspended, and when the number is 0 o'clock, it wakes up.) That is, how many times the thread hangs and how many times it takes to wake up. At the same time, it will keep the address pointer of the thread unchanged, so the thread hangs and then wakes up, and will begin to run from the place where it hangs.
destructor (clears the memory occupied by the thread): Destroy
Terminating thread Terminate

The use of this class is also very simple, the basic usage is: Derive a own thread class from TThread (because TThread is an abstract class, cannot generate an instance), and then an override (override) Abstract method: Execute (This is the thread function, This is the part of the code that executes in the thread), and if you need to use a visual VCL object, you need to go through the synchronize process.
Termination and exit of the thread:
1) Automatic exit:
When a thread exits from the Execute () process, meaning that the thread terminates, the ExitThread () function of Windows is called to clear the stack that the thread occupies.
If the Freeonterminate property of the thread object is set to True, the thread object is automatically deleted and the resource occupied by the thread is freed. This is the simplest way to eliminate threading objects.
2) controlled exit:
The Terminate property of a thread object can be used by a process or by another thread to control the exit of a thread.   Simply call the thread's Terminate method and set the thread object's Terminate property to True. In general, you should constantly monitor the value of terminate in a thread, and exit when it is found to be true, generally, such as in the Execute () procedure:
While not Terminate does begin ... end;
3) Exit the API function:
The API function declaration on thread exit is as follows:
Function TerminateThread (Hthread:thandle;dwexitcode:dword);
However, this function causes the code to terminate immediately, regardless of whether the program has
Try....finally
mechanism, which may lead to errors, is not a last resort, preferably not used.

4) using the Suspend thread method (suspend)
Using the Suspend method of the suspended thread, followed by a free, you can also release the thread, for example:
Thread1.suspend; Hang Up
Thread2.free; Release
two, multi-threaded synchronization mechanism
Synchronization mechanism, the necessity of studying the synchronization mechanism of multithreading is that when multithreading synchronously works, if the same resources are called simultaneously, problems can occur, such as the global variables, database operations conflict, and even deadlock and competition problems.
Take a look at the conflicting example:
In general, the operation of the memory data plus one of the three steps after decomposition: 1, read data from memory 2, data plus 13, memory is now assumed to be in a two-thread application with the addition of an Inc can be a situation: 1, thread A read the data from memory (assuming 3) 2, Thread B reads the data from memory (also 3) 3, thread A adds one to the data (now 4) 4, thread B adds one to the data (now 4) 5, thread A stores the data in memory (now the data in memory is 4) 6, thread B also stores the data in memory (now in-memory data or 4, but two threads add a , it should be 5, so there's a bad result here)
1. Critical section (Critical Sections)
A critical section (CriticalSection) is a technology that shares data access protection. There are only two operations on it: Enter and leave, and these two operations are atomic operations.
The principle of protection is this: When a thread A calls a certain enter, and then begins to access a data D, if another thread B also accesses data d, it will call this enter when it finds that the thread has entered the critical section, and then threads B is suspended, waiting for thread A to call leave. When thread a finishes operation, the call leave leaves, thread B is awakened, and the critical section flag is set to start manipulating the data, preventing access violations
var cs:trtlcriticalsection;//is declared at the top of the program as a global variable that can be used by threads. InitializeCriticalSection (CS); Initialization
Procedure InterlockedIncrement (var avalue:integer);     Begin entercriticalsection (CS);//Exclusive INC (Avalue); LeaveCriticalSection (CS); Release exclusive End;
Now look at the previous example: 1. Thread A enters the critical section (assuming data is 3) 2. Thread B enters the critical section because a is already in the critical section, so B is suspended 3. Thread A adds one to the data (now 4) 4. Thread A leaves the critical section and wakes up thread B (now the in-memory data is 4) 5. Thread B wakes up and adds one to the data (now 5) 6. Thread B leaves the critical section and the data is now correct. This is how the critical section protects access to shared data
Note that the critical section can only be used within one process, and it is possible to set the call enter in multiple places.
Do not lock a resource for a long time, and if you keep the resource locked, you will stop other threads from executing and take the entire program to a completely stopped state, so don't call sleep () or any wait in a ciritical section ... () function.
One drawback of the ciritical section is that it is not a core object, and if the thread that enters Ciritical section ends or is dropped, without calling LeaveCriticalSection, the system has no way of ciritical Section clears, if you need such a function, you should use mutexes.
VOID InitializeCriticalSection (
Lpcritical_section lpcriticalsection//A pointer to the critical_section variable to be initialized
);
function function: Initializes a critical object that must be called deletecriticalsection () to clear when you are using a completed critical object.
VOID deletecriticalsection (lpcritical_section lpcriticalsection//critical object pointer);
function function: Request to delete the critical object
VOID EnterCriticalSection (
Lpcritical_section lpcriticalsection//Critical Object pointer
);
function function: Request to enter the critical object
VOID LeaveCriticalSection (
Lpcritical_section lpcriticalsection//Critical Object pointer
);
function function
Request entry to Critical object
2. Mutex (mutexes)
Only one thread can have a mutex in a single time, as if only one thread can enter the same critical section at the same time.
The mutex and the critical section are still different:
1. Lock an unused mutexes, which takes almost 100 times times more time than locking an unused critical section
2. Mutexes can be used across processes, critical section can only be used in the same process
3. When waiting for a mutexes, you can specify the length of time to end the wait, but not for the critical section.
Correlation function Comparison of two objects:


The use mechanism of mutexes:
1. There is a mutex, at which time no thread owns it, and it is in a non-excited state.
2. A thread calls WaitForSingleObject () or any other wait ... function, and specifies that the handle of the mutex is a parameter
3. Win32 then gives the mutex the ownership of the thread, then sets the mutex to the firing state, and then wait: function returns
4. Wait: Once the function returns, the Win32 immediately sets the mutex to a non-firing state, and any other thread in the waiting state has no way of acquiring its ownership
5. The thread that obtains the mutex calls release and releases the mutex. So loop to the first step.
If a thread has a mutex and no call is made before the end of the Releasemutex,mutex is not destroyed, the mutex is considered "not owned" and "not fired" by Win32, and the next waiting thread is notified by WAIT_ABANDONED_0. If it is a waitformultipleobjects () waiting mutex, the function return value is between Wait_abandoned_0 and Wait_abandoned_0+n, and N is the number of elements in the handle array.
HANDLE CreateMutex (lpsecurity_attributes lpmutexattributes, BOOL Binitialowner, LPCTSTR lpname);
Parameters
Lpmutexattributes: Security attribute. Null means the default property is used.
Binitialowner: If you want the thread that calls this function to have a mutex, set this value to True
Lpname: Name of the mutex object
return value
If successful, returns a handle, otherwise returns NULL.
Function Description:
If the specified mutex name already exists, Win32 will give you a mutex handle instead of creating a new mutex for you. Calling GetLastError will return error_already_exists. When you don't need a mutex, you can call CloseHandle () to close it.
BOOL ReleaseMutex (
HANDLE Hmutex//HANDLE to release the mutex
);
return value
Returns true if successful, otherwise false is returned.
3. Semaphore (semaphores)
A mutex is a degenerate of semaphore, and if you produce a semaphore and make the maximum value 1, then it is a mutex. Thus, mutexes are often referred to as binary semaphore. In many systems, semaphore is often used because the mutex may not exist, and the semaphore used in Win32 is much less because of the reason why the mutex exists.
Once the present value of semaphore drops to 0, it means that the resource has been exhausted. At this point, if any thread calls wait ... function, it is necessary to wait until a lock is lifted.
HANDLE CreateSemaphore (
Lpsecurity_attributes Lpsemaphoreattributes,
LONG lInitialCount,
LONG lMaximumCount,
LPCTSTR lpname
)
Parameters:
Lpsemaphoreattributes: security attribute, NULL means use default property.
lInitialCount: Initial value, must >=0, and <= lMaximumCount
lMaximumCount: Maximum value, that is, the number of threads that can lock semaphore at the same time
Lpname: Name, this value can also be null.
return value:
Returns a handle if successful, otherwise returns NULL. If the semaphore name already exists, the function will still succeed and GetLastError will return error_already_exists
Function Description:
produce a semaphore.
BOOL ReleaseSemaphore (
HANDLE Hsemaphore,
LONG lReleaseCount,
Lplong Lppreviouscount
);
Parameters:
The handle to the Hsemaphore:semaphore.
Lreleasecount:semaphore the increment of the present value, usually 1, which cannot be a negative value or 0
Lppreviouscount: Returns the present value before the semaphore increment
return value:
Returns true if successful, otherwise false.
Function Description:
Iii. Event (events)
An event is a core object whose sole purpose is to become a state of excitation or non-excitation. These two states are completely under your control, not because of wait ... Changes to the function's invocation.
HANDLE CreateEvent (lpsecurity_attributes lpeventattributes, BOOL bManualReset, BOOL Binitialstate,
LPCTSTR lpname
);
Parameters:
Lpeventattributes: security attribute, NULL means use default property.
bManualReset:
This value is False, which indicates that the event is automatically reset to the non-excited state after it becomes the firing state;
This value is true, which means that it is not automatically reset and must be done by the program (call resetevent) to reset the event that fires state to a non-firing state.
Binitialstate: Initial state, true starts in excitation state, false starts in non-excited state
Lpname:event Object Name
return value:
Returns a handle if successful, otherwise returns NULL. If the event name already exists, the function will still succeed and GetLastError will return error_already_exists
BOOL SetEvent (HANDLE hevent);
Set the event object to the firing state
BOOL resetevent (HANDLE hevent);
Set the event object to a non-excited state
BOOL PulseEvent (HANDLE hevent);
If event bManualReset is true: Sets the event object to the firing state, wakes all waiting threads, and then the event reverts to the non-excited state
If the event's bManualReset is false: Sets the event object to the firing state, wakes up a waiting thread, and the event reverts to the non-excited state
5. Using the Synchronize method
This method is used to access the resources managed by the main thread of the VCL, and its method is: the first step is to put the code that accesses the main window (or the main window control resource) into a method of the thread; The second step is to use the method in the Execute method of the Threads object by using the Synchronize method. Example: Procedure Theater.execute; Begin Synchronize (update); End
Procedure Theater.update; The Begin ... end;
This synchronizes the thread method update with synchronize.

6, using the VCL class look method
Within the components provided by the Delphi IDE, there are some objects that provide a thread synchronization mechanism that can be used directly by worker threads, such as Tfont,tpen,tbitmap,tmetafile,ticon. In addition, a very important control object called Tcanvas, provides a lock method for thread synchronization, when a thread uses this control object, the first call the lock method of the object, and then the control to operate, and then call the Unlock method, release the control between the control. For example: Canversobject.look; Try drawing finally canversobject.unlock; End {Use this protection mechanism to ensure that the unlock will be executed if there is no exception, or the deadlock will likely occur. In the multi-threaded design, you should pay attention to the problem of deadlock}
Iv. priority of the thread:
In the case of multi-threaded, generally based on the importance of the thread to perform the task, give the thread the appropriate priority, generally if the volume of the thread concurrently request CPU time, priority high thread priority.
Precedence class (Priority Class)
WIN32 provides four priority categories, each of which corresponds to a basic priority hierarchy.
Table 5-1 Priority Categories

Most programs use Normal_priority_class. The priority category is one of the properties of the process and can be adjusted and fetched using the Setpriorityclass and Getpriorityclass functions.
Priority levels
The priority hierarchy of threads allows you to adjust the relative importance of threads within the same process. There are seven levels of priority:
Table 5-2 uses the setthreadpriority and getthreadpriority functions to adjust and retrieve this value
Under Windows, the priority level of the thread is divided into 30 levels, while the TThread object in Delphi is relatively simple to divide the priority into seven levels. That is, an enumeration type ttthreadpriority is declared in TThread:
Type ttthreadpriority (Tpidle,tplowest,tplower,tpnormal, Tphight,tphighest,tptimecrital)
The corresponding is the lowest (the system is idle when active,-15), lower (-2), Low (-1), Normal (normal 0), high (1), Higher (2), highest (15). The priority attribute of the thread object can be set with the precedence: Threadobject.priority:=tthreadpriority (level);

BOOL SetThreadPriority (
HANDLE hthread,//handle of the thread that wants to adjust priority
int npriority//values shown in table 5-2
);
Int getthreadpriority (
HANDLE hthread//thread handle
);
The return value is the priority of the thread.

The basic concept of multithreading and Delphi Threading Object TThread Introduction

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.