Some experiences in C + + Builder multithreading programming

Source: Internet
Author: User
Tags finally block thread

Recently in writing a program to use more than a thread, so the CB under the multithreading have a certain degree of learning.

Now tell me some of your own experiences. The level is limited, write very rough, please forgive me.

CB relative to VC, under the CB to write multithreaded routines is very simple. Not only is there tthread this class in VCL. Encapsulates the Windows API for multithreading. I think it's more convenient for him to offer

The ability to directly access objects in the main VCL thread. Can be very easy and the main thread in the form, control

Deal. There is not much difference between a single thread and a single-threaded approach. Just have multiple threads to access the main thread

(for example, to access Stringgrid on the same form). Just use the thread synchronize method to invoke the code that accesses the main VCL thread (see Help specifically), so we don't have to worry about the access violation. And for multithreading synchronization and mutual exclusion, CB also on window programming those mechanisms are encapsulated. For example, the criticalsection encapsulation of the critical area is tcriticalsection. Event events are encapsulated as tevent. These classes are fairly simple and easy to use.

The following is what I think is more important points for your reference.

1. The Waitfor method of TThread. is to wait for a thread to return. Its return value can be set arbitrarily in this line Chengri. So that the thread that calls him is aware of his operation when the thread returns.

Do thread cleanup work in the TThread onterminate event. He is not part of the thread running.

It is part of the main VCL thread. So local variables (such as int __thread i) in which thread cannot be accessed

You can write the clear code here without having to take care of where the Excute () method is now executed.

So it looks a bit like the role of a finally block in C + +.

2. Tevent is very important. Implement synchronization of threads. The WaitFor (int Timeout) feature is similar to the

WINDOW API WaitForSingleObject (). The return value includes:

Where the parameter timeout can be set to infinite for permanent wait, but this way, the program is easily dead here.

Wrsignaled This event occurs (successful return).

Wrtimeout wait for timeout.

Wrabandoned the event object has been destroyed before the timeout period for the event arrives.

Wrerror in the process of waiting for an exception to produce, to know the specific error to see Tevent lasterror

Property.

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.