Mutual exclusion and synchronization of C++builder threads

Source: Internet
Author: User
Tags function prototype

Mutex control is intended to prevent a thread from using an object or global variable to conflict with other threads. The methods for implementing thread mutex are:(1) The access code is delegated to the VCL main thread execution. To invoke a visual method or access its properties in a thread, the execution code is delegated to the VCL main thread for execution, or a concurrent access violation occurs. The delegate's method is to first separate the code that uses the visual component into a function, the function prototype is the VOID function name (void), and then electrophoresis the member function of the TThread class synchronize (the function name) to invoke it, The VCL main thread sequence performs all build-to-build access (including response to HMI events, Windows system events, and so on) so that no conflicts occur. (2) Use object lock. Some VCL classes provide object locks that can be locked and unlocked using the object's lock and Unlock methods. When these objects are accessed, the lock method can be called to lock the object and then access the object, and the unlock method of the calling object is released when the object is accessed. (3) Use the critical section object. To access a global variable, you can set up a critical section object (tcritical) to implement the mutex, which has a acquire and release two methods. The acquire method blocks other threads, executes the critical section code, and the release method frees the thread waiting to enter the critical section. For example: Set Q as a global variable, CRIT1 is a critical section object, when access to the critical section of Q must execute Crit1.acquire (), after access to exit the critical section must be executed Crit1.release (). PS: The second method in practice indicates that the lock and unlock functions are not defined. The third method can be used, recommended.

Mutual exclusion and synchronization of C++builder threads

Related Article

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.