Learning Notes: Delphi multi-threaded knowledge has been reviewing the old knowledge, just learn Java thread safety knowledge, today think of the Delphi development has been done before, so it is necessary to review, to see what the different programming languages are different. Delphi's Thread synchronization method:1, the critical section affirms a critical resource flock:trtlcriticalsection; First, a critical resource object InitializeCriticalSection (FLock) Destroy critical Resource object DeleteCriticalSection (FLock)procedureTsavethread.pushtask (ptask:pfileinfo);begin //entering a critical resource, after the thread is occupied, the subsequent threads blockentercriticalsection (FLock); Try //here is the protected code if notAssigned (Ftasks) ThenFtasks:= TList.Create; Ftasks.add (Ptask); finally //leaving the critical resource, the other blocked threads can enterleavecriticalsection (FLock); End;EndThis approach is quite simple, similar to the synchronized code block in Java2, event events have two states: signal and no signal, which can be used for thread synchronization. The event-related APIs are also more flexible to control and look at several of his methods: CreateEvent: Create event SetEvent: Set to signaled state resetevent: set to No signal status PulseEvent: Set to signaled state, Then it is set to no signal state (somewhat similar to the sense of a single step Debug) in the thread or with WaitForSingleObject to blockprocedureTsearchtopnthread.execute;begin while notself.terminated Do beginWaitForSingleObject (fevent, INFINITE);//Fevent block when signal is not signaled, until the code continues to execute when it becomes signaled ....... End;End;//Set event status to no signal, block threadprocedureTform1.button1click (sender:tobject);beginresetevent (hevent);End;//Set event status to signaled, thread unblockedprocedureTform1.button2click (sender:tobject);beginSetEvent (hevent);End;//set the pulse signal State, first set the signal, and then set no signalprocedureTform1.button3click (sender:tobject);beginPulseEvent (hevent);End; 3, mutex objectsvarHmutex:thandle;//handle to a mutex objectfunctionThreadfun (p:pointer): DWORD;stdcall;vari:integer;beginfor I:=0 to + Do begin //use WaitForSingleObject to access the mutex object, if the Hmutex is locked >=1, then wait, if it is unlocked then give Hmutex lock, and then run the following code ifWaitForSingleObject (Hmutex, INFINITE) = Wait_object_0 Then begin //Code in Protection ..... //after the code ends, the semaphore is released, indicating that the current thread has finished processing and that the subsequent thread can gain controlReleaseMutex (Hmutex); End; End; Result:=0;End;proceduretform1.formcreate (sender:tobject);beginHmutex:= CreateMutex (Nil, False,Nil);//The second argument is false to indicate that the mutex created is not owned by any thread, and that the initial state is unlocked and the thread can occupyEnd;procedureTform1.formdestroy (sender:tobject);beginCloseHandle (Hmutex);End;Endthe total process and the critical section are somewhat similar, except that the mutex object is a system core object that can span a process, while a critical section is just the current process. This feature can be used for concurrency protection of resources such as files. 4, semaphores are similar to mutually exclusive objects, and the semaphore is characterized by the ability to set a count, which allows multiple threads to access the same piece of code simultaneously. A mutex is the case where the semaphore is 1, and the subsequent thread can only wait until the previous one executes, and the semaphore allows multiple executions to be specified. Later, when looking at TThread's code, I found a good thing in the new Delphi version: TmnitorTryTmonitor.enter (Threadlock); Trysyncprocptr.queued:=queueevent; ifSynclist =Nil Thensynclist:= TList.Create; Syncprocptr.syncrec:=Asyncrec; Synclist.add (SYNCPROCPTR); Signalsyncevent; ifAssigned (Wakemainthread) ThenWakemainthread (SyncProcPtr.SyncRec.FThread); if notQueueevent Thentmonitor.wait (syncprocptr.signal, Threadlock, INFINITE)finallyTmonitor.exit (Threadlock); EndThis code is similar to the synchronized code block in Java, so it is much easier to write thread synchronization.
Go: Learn Notes: Delphi Multi-threaded learning