Multi-threaded programming in C ++ Builder.

zhaozj2021-02-17  85

Chongqing University of Posts and Telecommunications Computer No. 98 Deng Rui

2001.11

Recently I wrote a program for multi-thread, so there is a certain study for multi-threads under CB. Now talk about some of yourself. The level is limited, it is very rough, please forgive me.

CB is very simple to write multi-threaded programs under CB relative to VC. Not only is a TTHREAD in the VCL. Encapsled those Window APIs about multithreading. I don't think it is that he provides the ability to directly access objects in the primary VCL thread. You can easily deal with the forms, controls in the main thread. And the way the single thread doesn't have much difference. Just access to multiple threads to access objects in the main thread (such as a StringGrid on the same form). Just use the THREAD's SYNCHRONIZE method to call the code to access the main VCL thread (for specific help), we Don't worry about accessing conflicts. Moreover, for multi-threaded synchronization and mutual exclusion, CB also encapsulates those mechanisms in Window programming. For example, the critical area criticalSECTION is packaged for TCRITicalSECTION. Event Event is packaged to tevent. These classes are quite simple. Here is what I think is more important for your reference.

1. TTHREAD Waitfor method. Is waiting for a thread to return. Its return value can be set any in this thread. To let the thread call his thread know his operation when returning. Make thread clearance work in TTHREAD's onterminate event. He is not part of the thread run. It is part of the main VCL thread. So in which the local variables you can't access Thread (such as int __thread i) you can write a clear code here, don't use it now in the Excute () method which place is executed. This seems a bit similar to the role of the Finally block in C . 2. TEVENT is very important. Implement the synchronization of threads. Waitfor (int Timeout) is similar to Window API WaitForsingleObject (). Return Values ​​include: where parameter Timeout can be set to Infinite to represent permanent waiting, but in this way, the program is easy to die. WRSIGNALED This event occurred (successfully returned). Wrtimeout waits for timeout. Wrabandoned is already destroyed before the timeout period arrived. Wrerror has an abnormality in the waiting process, you know the specific error to see the TEVENT's LastError property. 3 TcriticalSECTION This is equivalent to the critical area in Win32 programming. In multi-threaded programming, multiple threads need to access the same common variable. To ensure the correctness of access. The code access to public variables is written between Enter (); and between Leave (). For example, there is a public variable count;: tcriticalsection * psection = new tcriticalSECTION (); psection-> enter (); count ; psection-> leave (); delete p; enter () method enters the critical area, the public Variables are locked. The Leave () method leaves the critical area and unlocks the common variables therein. 4.TmultireadexClusiveWritesynchronizer is used to handle problems similar to multiple producers and multiple consumers. The consumer here refers to a thread that reads a public variable. The producer is a thread that writes a common variable.

Four methods. BeginRead endread These two methods are used for consumers. BEGINWRITE ENDWRITE These two methods are used for producers.

It is to use this TMUTIREXCLUSIVEWRITESYNCHRONIREDEXCLUSIVEWRITESYNCHRONIZER to define a global variable. Then visit him in other threads.

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

New Post(0)