C++11 Thread Condition variable condition_variable (ii)

Source: Internet
Author: User
Tags mutex

Title: Write a program, open 3 threads, the IDs of the 3 threads are a, B, C, each thread prints its own ID on the screen 10 times, the output must be displayed in the order of ABC, such as: Abcabc .... recursion in turn.

Implemented with C++11:

[CPP]View Plaincopyprint?
  1. #include <iostream>   
  2. #include <thread>   
  3. #include <mutex>   
  4. #include <condition_variable>   
  5. using   namespace std;
  6. Mutex m;
  7. Condition_variable cond;
  8. int  loop=10;
  9. int  flag=0;
  10. void Fun (int ID) {
  11. for(int i=0;i<loop;i++) {
  12. Unique_lock<mutex> LK (m);
  13. while ( id!=flag)//Must be judged by the loop, if multiple blocking threads wake up and are in the critical section
  14. Cond.wait (LK);
  15. cout<< (U_char) (' A '+id) << " "  ;
  16. Flag= (flag+1)%3;
  17. Cond.notify_all ();
  18. }
  19. }
  20. int Main () {
  21. thread B (fun,1);
  22. thread C (fun,2);
  23. Fun (0);
  24. cout<<endl;
  25. B.join ();
  26. C.join ();
  27. return 0;
  28. }

Summary: This problem with a slightly more complex, because it has a thread, more than a thread problem comes, assuming that the cable path ABC, we want to let a first execute a part of the code, bcwait, this good to do, through the conditional variable let BC Wait, a after the BC all received the signal, this time the BC simultaneously execute? Do not handle the words are actually executed at the same time, but the question is to ensure that B first, it is important to note the red area of the code above, through while to determine: When a executes, we want to execute B, that requires a to change the flag after the mark, here is the addition of 1, when the BC received a signal continue to execute, Because while the existence, but also to judge, the result of this judgment is B jump out while, and C continue to loop stop in wait place, similarly, b after execution, the same flag, and then C jump out of the loop, so that the control 3 threads order effect. However, depending on the requirements, it is necessary to change the strategy, but the mutex, condition_variable, flag three global variables are necessary to achieve the control goal, while the other is also the essential technique to realize the control, and the other uncertain techniques are basically using while implementation.

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.