Illustration C ++ Multithreading

Source: Internet
Author: User

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:

 
 
  1. Void _ fast call TProducerThread: Execute ()
  2. {
  3. // ---- Place thread code here ----
  4. IntI=0;
  5. Int j;
  6. While (I<100) // Each producer thread produces 100 items
  7. {
  8. Sleep (1000); // latency, for clear display of execution results
  9. If (Form1->Buffer_size>0) // The buffer pool is not empty, notifying consumers of consumption
  10. {
  11. Form1->Begin Consumer->Set Event ();
  12. }
  13. Form1->Produce Guard->Acquire ();
  14. I ++;
  15.  StrResult=IntToStr(I );
  16.  J=Form1->Buffer_size;
  17. Form1->Product [j] = I;
  18. Form1->Buffer_size ++;
  19. Synchronize (Show Result); // refresh the page to display the latest production-consumption status
  20. Form1->Begin Consumer->Set Event (); // notifies consumers of consumption
  21. If (Form1->Buffer_size= 5) // when the buffer pool is full, the producer thread is suspended until the notification is reproduced.
  22. {
  23. Form1->Begin Produce->Wait For (INFINITE );
  24. }
  25. Sleep (1000 );
  26. Form1->Produce Guard->Release ();
  27. }
  28. While (Form1->Buffer_size>0)
  29. {
  30. Form1->Begin Consumer->Set Event ();
  31. }
  32. }

Consumer thread:

 
 
  1. Void _ fast call TConsumerThread: Execute ()
  2. {
  3. // ---- Place thread code here ----
  4. Int j;
  5. For (intI=0; I< 200; I ++)
  6. {
  7. Sleep (100); // latency, for clear display of execution results
  8. Form1->Begin Consumer->Wait For (INFINITE); // suspends the consumer thread until the notification is consumed again.
  9. J=Form1->Buffer_size-1;
  10. StrResult=IntToStr(Form1->Product [j]);
  11. Form1->Buffer_size --;
  12. Synchronize (Show Result); // refresh the page to display the latest production-consumption status
  13. 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.
  14. {
  15. Form1->Begin Produce->Set Event ();
  16. }
  17. Sleep (100 );
  18. }
  19. }

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.

  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.