Notes for using C ++ Builder

Source: Internet
Author: User

As an efficient C ++ programming language, C ++ Builder combines function language and object-oriented programming language. It is perfect for programming, algorithms, technology, and exploratory development, therefore, you can feel interesting and attractive in the use process.

Note that the thread object cannot control the thread stack size or its security attributes. To control this, you must use the Create Thread () or Begin Thread () function of WindowsAPI. The TThread class has the following attributes and methods:

1) attributes:

◆ Priority: Priority attribute. You can set the thread priority.

◆ Return Value: Return Value attribute. When the thread is introduced, a value is returned to other threads.

◆ Suincluded: suspends the property. You can determine whether a thread is suspended.

◆ Terminated: End attribute. Used to indicate whether the thread should be terminated.

◆ ThreadID: Id property. The ID of the thread throughout the system. This attribute is very useful when using Windows API functions.

2) method:

◆ Do Terminate: generates an On Terminate event, but does not end thread execution.

◆ Resume: Wake up a thread to continue execution.

◆ Suspend: suspends a thread and must be used in pairs with the Resume process.

◆ Synchronize: A synchronization process called by the main C ++ thread.

◆ Terminate: Set the Terminate attribute to True to abort the thread execution.

◆ Wait For: Wait For the thread to stop and Return the Value of the Return Value attribute.

2. Coordinating threads

When writing code run by a thread, you must consider the behavior of other threads that may be executed synchronously. Note: avoid two threads trying to use the same global object or variable at the same time. In addition, the Code in one thread depends on the results of tasks executed by other threads.

1) avoid simultaneous access

To avoid conflicts with other threads when accessing global objects or variables, you may need to pause the execution of other threads until the thread code completes the operation.

(1) Lock the object. Some objects have built-in locking functions to prevent other threads from using the instance of this object. For example, the canvas object TCanvas and Its Derived classes) has a Lock function that prevents other threads from accessing the canvas until the Unlock function is called. Obviously, this method is only valid for partial classification.

(2) Use important segments. If the object does not provide the built-in locking function, you can use the important section. Like a door, an important section only allows one thread to enter each time. To use an important section, you must create a global instance of TCriticalSection. TCriticalSection has two functions: Acquire) to prevent other threads from executing the region) and Release) to cancel blocking of other threads ).

(3) Use a multi-read, exclusive write synchronization. When an important segment is used to protect the global memory, only one thread can use the memory at a time. This protection may exceed the requirements, especially when there is an object or variable that is frequently read but rarely written. Multiple Threads read the same memory at the same time, but there is no thread to write the memory.

The TMultiReadExclusiveWriteSynchronizer object can protect global variables that are frequently read but rarely written. This object is the same as the important section, but it allows multiple threads to read at the same time, as long as there is no thread to write. Every thread that needs to Read the memory first needs to call the Begin Read) function to ensure that no other threads write the memory). After the thread completes the Read operation to protect the memory, it needs to call the End Read) function. The Begin Write function must be called by any thread to ensure that no other threads read or Write the memory). After the Write operation on the protected memory is completed, the End Write function is called.

(4) use the Synchronize function: Void _ fast call Synchronize (TThreadMethod & Method );

The parameter Method is a process name without parameters. In this process without parameters, there are some code for accessing VCL. We can call the Synchronize process in the Execute Process to avoid concurrent access to VCL. The main thread is notified by the Synchronize process.

The main thread then executes the process without parameters in the parameter list of the Synchronize process at the appropriate time. In the case of multiple threads, the main thread puts the notifications sent from the Synchronize process into the message queue, and then responds to these messages one by one. This mechanism Synchronize achieves synchronization between threads.

2) Wait for other threads

Use C ++ Builder to temporarily interrupt the thread execution if the thread must wait for another thread to complete a task. Then, either wait for the completion of the other thread, or wait for the other thread to know that the task has been completed.

(1) wait until the thread execution ends.

Wait until the execution of another thread ends and use the Wait For function. The Wait For function is returned only when the thread is terminated. The method of termination is either completed by the Execute function or because of an exception.

(2) wait until the task is completed. Sometimes, you only need to wait for the thread to complete some operations, rather than waiting for the thread to finish the execution. To this end, you can use an event object. Event object TEvent) should have a global scope so that they can be visible to all threads.

Call the TEvent: Set Event function when a thread is dependent on other threads. Set Event sends a signal so that other threads can check and learn that the operation is complete. To turn off the signal, use the Reset Event () function.

For example, when you have to wait for several threads to complete their execution rather than a single thread. Because I do not know which thread is finished at the end, when using the C ++ Builder function, you can call the Set Event to accumulate the Count value at the end of the thread and send a signal at the end of the last thread to indicate that all threads end.

  1. How to Write C ++ project development and project plan correctly
  2. Summary Notes on learning and exploring C ++ library functions
  3. In-depth demonstration of high security of C ++
  4. Describes in detail how to accurately Write C ++ languages.
  5. In-depth demonstration of high security of C ++

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.