Trtlcriticalsection is a struct, defined in a Windows cell; initializecriticalsection, EnterCriticalSection, LeaveCriticalSection, deletecriticalsection and so on these several kernel32.dll in the critical section operation API parameters; Tcriticalsection is the class implemented in the SYNCOBJS unit, It encapsulates the above critical section Operations API functions, simplifying and facilitating the use of Delphi, such as Tcriticalsection.enter, which calls the EnterCriticalSection API function. In a multithreaded program, if each thread accesses the same resource, as if a variable were to use thread synchronization technology, there are many ways to create conflicts between threads and interfere with thread synchronization, and using a critical section is the simplest and most efficient method (with the least CPU time) to use the critical section code as follows: Declare a global variable of type trtlcriticalsection firstvarmycs:trtlcriticalsection; Initialize the InitializeCriticalSection (MYCS) before the program starts or creates a thread;//Initialize critical sectionDelete it deletecriticalsection (MYCS) After the end of the program or all threads;//Delete a critical sectionadd EnterCriticalSection (MYCS) where you want to synchronize in the thread;//Enter the critical section Try //Program Code finallyleavecriticalsection (MYCS);//leave the critical section End;
Goto-critical Section object Tcriticalsection differs from Trtlcriticalsection