C ++ Builder master advancement (5) Write multi-threaded applications with BCB

zhaozj2021-02-16  47

C builder master advanced

(5) Write multi-threaded applications with BCB

nxyc_twz@163.com

With the global popularity of the Windows system, multi-threading technology has become more and more software designs. Using multi-threading technology can fully improve the performance efficiency of the application. Previously, in order to achieve multi-threaded programming, it is basically called a series of API functions, such as CreateThread, ResumeThread, etc., is not easy to control, it is easy to errors. After using BCB, I found that it is so simple to write multithreaded programs! BCB provides us with a powerful TTHREAD class, making multi-threaded programming very easy. Please start with me to start our BCB multi-threaded program. 1. Create a multi-thread program: First, I will introduce the specific steps to write multithreaded programs in the BCB. In the C Builder IDE environment, select menu file | New, select Thread Object in the New column, press OK, then pop up the input box, enter the name of the TTHREAD object subclass, so C Builder automatically creates a TTHREAD called TNewthread for you. Subclass. The following is a partial TNewThread Source: __fastcall NewThread :: MyThread (bool CreateSuspended): TThread (CreateSuspended) {// Constructor, some of the information may be used to initialize} void __fastcall NewThread :: Execute () {// Core multithreaded program, Used to perform related multi-threaded operations}

The execute () function in the BCB is the place where we want to implement in the thread. When using multiple threads, dynamically create a TNewThread object, using the resume () method in the constructor, the specific execution code uses the execute () method to overload the code. If you want to create more threads, just create a number of TNewThread. Through the above steps we basically realize how to create a thread in the program, and make the program implement a multi-threaded application. However, the implementation of multi-threaded applications is not a simple job, but also considers a lot of factors that make multiple threads can coexist and mutually affecting each other. For example, the public variables in the program, the distribution of resources, if handled properly, not only the thread will die, but may even cause the system to crash. In general, pay attention to the processing of shared objects and data in multi-threaded programming, cannot be ignored. So, below we want to talk about the common problems in multi-thread: 2. We all know the use of VCL objects in multithreading, C Builder programming is based on the VCL class library. The properties and methods of accessing VCL objects often need to be accessed in the program. Unfortunately, the VCL class library does not guarantee that the properties and methods of the object are thread access secure (thread_safe), accessing the attribute of the VCL object or calling its method, which may access the memory area that is not protected by other threads. error. Therefore, the TTHREAD object provides a synchronize method that avoids conflicts when accessing the VCL object attribute or calling a method in the thread, avoid conflicts with the Synchronize method to avoid conflicts, so that each thread is coordinated without accidents. error. As follows: void __fastcall TNewThread :: PushTheButton (void) {Button1-> Click ();} void __fastcall TNewThread :: Execute () {... Synchronize ((TThreadMethod) PushTheButton); ...} of Button1-> The call to the click () method is implemented by the Synchronize () method, which automatically avoids multithreading conflicts. In C Builder, although some VCL objects are also thread access secure (such as TFont, TPEN, TBRUSH, etc.), you can use the Sychronize () method to access the property method to improve program performance, but for more Determined VCL objects, but also strongly recommend using the Synchronize () method to ensure the reliability of the program. 3. The usage programming of public data in multi-thread is inevitably share data or objects in multiple threads. In order to avoid catastrophic consequences in multithreading due to simultaneous access to public data blocks, we need to protect public data blocks until a thread ends. This can be implemented through the use of critical sections, and fortunately, in C Builder, it provides us with a TCRiticalSECTION object to make a critical area. This object has two methods, acquire () and release (). It sets the critical area to ensure that only one thread is accessible once.

转载请注明原文地址:https://www.9cbs.com/read-26438.html

New Post(0)