C++的多线程编程(练习一下condition_variable)

        嗯,高考结束了,那就编写一个阅卷和查成绩的多线程吧。一个线程老师阅卷,其他三个线程查成绩。代码如下:

       

  1 #include <iostream>
  2 #include <thread>
  3 #include <mutex>
  4 #include <condition_variable>
  5 #include <chrono>
  6 #include <future>
  7 #include <map>
  8 
  9 // 定义一个简单的分数存储结构
 10 typedef std::map<std::string, double> RecordType;
 11 
 12 std::mutex ready_mutex;
 13 std::condition_variable ready_condition;
 14 RecordType record_table;
 15 
 16 //模拟成绩查询
 17 void query_score(std::string name)
 18 {
 19     bool need_wait = true;
 20     std::cout << name << ": Now start query score..." << std::endl;
 21     while (need_wait)
 22     {
 23         // 等待条件变量,大括号限制互斥量的作用域,便于自动析构
 24         {
 25             std::unique_lock<std::mutex> unqique_lock_mutex(ready_mutex);
 26             // 设置等待超时30s
 27             std::cv_status status = ready_condition.wait_for(unqique_lock_mutex, std::chrono::seconds(30));
 28             if (std::cv_status::no_timeout == status)
 29             {// 如果还没有超时
 30                 // 开始查询成绩
 31                 auto record = record_table.find(name);
 32                 if (record != record_table.end())
 33                 {//查询到了对应的成绩,就输出
 34                     double score = record_table[name];
 35                     std::cout << name << "'s record is " << score << std::endl;
 36                     need_wait = false;
 37                 }
 38                 else
 39                 {// 查不到成绩,就再次等待
 40                     continue;
 41                 }
 42             }
 43             else
 44             {// 如果等到花儿也谢了
 45                 std::cout << name << "'s record maybe lost!!!" << std::endl;
 46                 need_wait = false;
 47             }
 48         }
 49     }
 50     std::cout << name << ": Now finish query record." << std::endl;
 51 }
 52 
 53 void rate_score()
 54 {
 55     // 老师开始批卷
 56     std::cout << "Now the teacher start rating...." << std::endl;
 57     // 批卷一个同学要5秒,神速
 58     std::this_thread::sleep_for(std::chrono::seconds(5));
 59     // 批改完毕,上传成绩
 60     {
 61         std::lock_guard<std::mutex> lock_guard_the_mutex(ready_mutex);
 62         record_table.insert({ "Lucy", 550.5 });
 63     }
 64     // 上传完毕,需要知会一下正在查询的同学
 65     ready_condition.notify_all();
 66     std::this_thread::yield();
 67 
 68     // 继续批改下一个同学
 69     std::this_thread::sleep_for(std::chrono::seconds(6));
 70     // 再次提示,大括号是为了互斥锁的自动析构
 71     {
 72         std::lock_guard<std::mutex> lock_guard_the_mutex(ready_mutex);
 73         record_table.insert({ "Han Meimei", 655.0 });
 74     }
 75     ready_condition.notify_all();
 76     std::this_thread::yield();
 77 
 78     // 继续批改下一个同学
 79     std::this_thread::sleep_for(std::chrono::seconds(7));
 80     // 可以看出来老师批改越来越慢了
 81     {
 82         std::lock_guard<std::mutex> lock_guard_the_mutex(ready_mutex);
 83         record_table.insert({ "Li Lei", 635.0 });
 84     }
 85     ready_condition.notify_all();
 86     std::this_thread::yield();
 87 
 88     // 老师累了,批完闭眼休息一下,下班了
 89     std::this_thread::sleep_for(std::chrono::seconds(5));
 90     std::cout << "Now the teacher finish rating." << std::endl;
 91 }
 92 
 93 
 94 int main()
 95 {
 96     // 各位同学开始查询成绩
 97     auto thread1 = std::thread(query_score, "Li Lei");
 98     // Han Meimei的手速慢了
 99     std::this_thread::sleep_for(std::chrono::seconds(1));
100     auto thread2 = std::thread(query_score, "Han Meimei");
101     // Lucy在国外,网络延迟了
102     std::this_thread::sleep_for(std::chrono::seconds(3));
103     auto thread3 = std::thread(query_score, "Lucy");
104 
105     // 老师开始批卷
106     auto thread4 = std::thread(rate_score);
107 
108     // 等待成绩查询完毕和批卷完毕
109     thread1.join();
110     thread2.join();
111     thread3.join();
112     thread4.join();
113 
114     return 0;
115 }

 

以下是运行结果:

        知道这三位同学的同学,年纪应该都比较大了。

  

posted @ 2023-06-10 16:02  颜秋哥  阅读(48)  评论(0编辑  收藏  举报