Thread Synchronization Technology.
1. critical sections (critical segment). If there is a part in the source code that cannot be executed simultaneously by two or more threads, you can use the critical segment to serialize the code. It can only be used in an independent process or an independent application. The usage is as follows:
// Create a form
Initializecriticalsection (critical1)
// Destroy the form
Deletecriticalsection (critical1)
// Thread
Entercriticalsection (critical1)
...... Protected code
Leavecriticalsection (critical1)
2. mutex (mutex object) is a global object used for serializing access to resources. We first set the mutex object, then access the resource, and finally release the mutex object. When a mutex object is set, if another thread (or process) tries to set the same mutex object, the thread will stop until the previous thread (or process) releases the mutex object. Note that it can be shared by different applications. The usage is as follows:
// Create a form
Hmutex: = createmutex (nil, false, nil)
// Destroy the form
Closehandle (hmutex)
// Thread
Waitforsingleobject (hmutex, infinite)
...... Protected code
Releasemutex (hmutex)
3. semaphore (semaphore), which is similar to a mutex, but can be counted. For example, a given resource can be simultaneously accessed by three threads. In fact, mutex is the semaphore with the largest count. The usage is as follows:
// Create a form
Hsemaphore: = createsemaphore (nil, linitialcount, lmaximumcount, lpname)
// Destroy the form
Closehandle (hsemaphore)
// Thread
Waitforsingleobject (hsemaphore, infinite)
...... Protected code