Differences between tcriticalsection (Delphi) and trtlcriticalsection

Source: Internet
Author: User
The trtlcriticalsection is a struct defined in a Windows Unit. It is the API parameter in the critical section of initializecriticalsection, entercriticalsection, and other kernel32.dll. The tcriticalsection is implemented in the syncobjs unit, it encapsulates the operation API functions in the preceding critical sections to simplify and facilitate the use of API functions in Delphi, such as tcriticalsection. enter; that is, the entercriticalsection API function is called. In multi-threaded programs, if each thread needs to access the same resource, such as a variable, thread synchronization technology is required to avoid conflicts between threads and there are multiple ways to interfere with thread synchronization, using the critical section is the simplest and most efficient method (with the least CPU usage). The following code uses the critical section: declare a global variable of the trtlcriticalsection type varmycs: trtlcriticalsection; initialize initializecriticalsection (MYCS) before the program starts or establishes a thread; // Delete the ecriticalsection (MYCS) after the program ends or all threads end ); // Delete the critical section and add the entercriticalsection (MYCS) to be synchronized in the thread; // enter the critical section try // program code finally leavecriticalsection (MYCS); // exit the end Of the critical section;

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.