The following describes C ++ multithreading. C ++ multithreading has completed multiple tasks for synchronization and greatly improved resource usage efficiency, to explain C ++ multithreading in simple words, it is to complete multiple tasks at the same time.
Important section TCriticalSection) and event TEvent) are applied. The producer notifies the consumer to start consumption through the Begin Consume object of the TEvent class, and the consumer notifies the producer to start production through the Begin Produce object of the TEent class. There are two producers and one consumer in the program. The TCriticalSection class is used to synchronize objects between two producers. Its running interface 1 is shown.
The main source code is as follows: Producer thread:
- Void _ fast call TProducerThread: Execute ()
- {
- // ---- Place thread code here ----
- IntI=0;
- Int j;
- While (I<100) // Each producer thread produces 100 items
- {
- Sleep (1000); // latency, for clear display of execution results
- If (Form1->Buffer_size>0) // The buffer pool is not empty, notifying consumers of consumption
- {
- Form1->Begin Consumer->Set Event ();
- }
- Form1->Produce Guard->Acquire ();
- I ++;
- StrResult=IntToStr(I );
- J=Form1->Buffer_size;
- Form1->Product [j] = I;
- Form1->Buffer_size ++;
- Synchronize (Show Result); // refresh the page to display the latest production-consumption status
- Form1->Begin Consumer->Set Event (); // notifies consumers of consumption
- If (Form1->Buffer_size= 5) // when the buffer pool is full, the producer thread is suspended until the notification is reproduced.
- {
- Form1->Begin Produce->Wait For (INFINITE );
- }
- Sleep (1000 );
- Form1->Produce Guard->Release ();
- }
- While (Form1->Buffer_size>0)
- {
- Form1->Begin Consumer->Set Event ();
- }
- }
Consumer thread:
- Void _ fast call TConsumerThread: Execute ()
- {
- // ---- Place thread code here ----
- Int j;
- For (intI=0; I< 200; I ++)
- {
- Sleep (100); // latency, for clear display of execution results
- Form1->Begin Consumer->Wait For (INFINITE); // suspends the consumer thread until the notification is consumed again.
- J=Form1->Buffer_size-1;
- StrResult=IntToStr(Form1->Product [j]);
- Form1->Buffer_size --;
- Synchronize (Show Result); // refresh the page to display the latest production-consumption status
- If (Form1->Buffer_size= 4) // The buffer pool is no longer full, and the producer thread suspended due to the buffer pool full is awakened.
- {
- Form1->Begin Produce->Set Event ();
- }
- Sleep (100 );
- }
- }
This article discusses C ++ multi-threaded programming and its feasibility, and explains the significance of C ++ multi-threaded programming in Windows, this topic focuses on how to develop multi-threaded applications on the C ++ Builder platform. By implementing the famous "producer-consumer problem" process synchronization problem, it clearly reflects the multi-threaded programming technology in Windows and its implementation functions and effects.
- How to Write C ++ project development and project plan correctly
- Summary Notes on learning and exploring C ++ library functions
- In-depth demonstration of high security of C ++
- Describes in detail how to accurately Write C ++ languages.
- In-depth demonstration of high security of C ++