首页 > 其他分享 >使用countDownLatch 实现 三体动漫中航天服检查流程

使用countDownLatch 实现 三体动漫中航天服检查流程

时间:2023-01-20 15:44:43浏览次数:52  
标签:printf muduo 三体 航天服 back latch vec countDownLatch include

 

 

 1 #include <boost/foreach.hpp>                 //-lboost_iostreams
 2 #include "muduo/base/CountDownLatch.h"
 3 #include "muduo/base/Thread.h"
 4 #include <iostream>
 5 #include <vector>
 6 #include <string>
 7 #include <vector>
 8 
 9 using namespace std;
10 
11 class TestFun
12 {
13     
14 public:
15     TestFun(int numThreads,vector<string>* p) : latch_(numThreads),pVector(p)
16       {
17           for (int i = 0; i < numThreads; ++i)
18         {
19               threads_.emplace_back(new muduo::Thread(std::bind(&TestFun::threadFunc,this),p->at(i)));
20         }                
21       }
22 
23       void run()
24     {        
25         for (auto& thr : threads_)
26         {
27               thr->start();
28         }
29         printf("all threads started\n");
30 
31         printf("waiting for count down latch\n");
32         printf("in run:%d->Count:%d\n",muduo::CurrentThread::tid(),latch_.getCount());
33 
34         latch_.wait();                                  //会原子unlock mutex并进入等待,wait执行完毕会自动重新加锁
35     }
36 
37     void threadFunc()
38       {
39         printf("tid=%d,开始检查:%s\n",muduo::CurrentThread::tid(),muduo::CurrentThread::name());
40            latch_.countDown();                              //必须在while前,启动一个减1,0会唤醒wait()启动完毕
41         printf("%d->Count:%d\n",muduo::CurrentThread::tid(),latch_.getCount());
42         bool running = true;
43            while (running)
44         {
45             sleep(1);              
46               running = false;
47         }    
48     
49         printf("tid=%d,检查完毕:%s \n",muduo::CurrentThread::tid(),muduo::CurrentThread::name());
50     }  
51 
52 
53     void joinAll()
54     {
55         for (auto& thr : threads_)
56         {
57           thr->join();
58         }
59 
60         printf("ALL thread finish\n");
61     }
62 
63 
64 private:  
65     muduo::CountDownLatch                       latch_;
66     std::vector<std::unique_ptr<muduo::Thread>> threads_;
67     vector<string>*                             pVector;  
68 };
69 
70 int main()
71 {
72     vector<string> vec;
73     vec.emplace_back("1.外骨骼关节轴承");
74     vec.emplace_back("2.辅助背包");
75     vec.emplace_back("3.防护层");
76     vec.emplace_back("4.隔热层");
77     vec.emplace_back("5.气密限制层");    
78     //BOOST_FOREACH(std::string s,vec) cout<<s<<endl;
79 
80     printf("Main: pid=%d, tid=%d\n", ::getpid(), muduo::CurrentThread::tid());
81     
82     
83       TestFun t(vec.size(),&vec);
84       t.run();
85       t.joinAll();    
86 
87     printf("舱外航天服全模块检查完毕\n");  
88 
89    
90     return 0;
91 }

备注:编译需链接 muduo库和boost库

-lmuduo_base -lboost_iostreams

标签:printf,muduo,三体,航天服,back,latch,vec,countDownLatch,include
From: https://www.cnblogs.com/sansuiwantong/p/17062803.html

相关文章

  • ISO认证是什么?ISO三体系认证有什么不同之处?
    说到ISO体系认证大家应该都非常的熟悉,ISO三体系认证中包含了ISO9001的质量管理体系认证、ISO14001的环境管理体系认证和ISO45001的职业健康安全管理体系,在之前的文章中龙翊......
  • CountDownLatch的使用
    一、介绍CountDownLatch是一个计数的闭锁,作用与CyclicBarrier有点儿相似。在API中是这样描述的:用给定的计数初始化CountDownLatch。由于调用了countDown()方法,所以......
  • cctv8被《三体》卡住了,网站打开根本看不了,估计已经破千万播放量了。我等着看回放呢。
    电视:cctv8频道:CCTV-8电视剧频道高清直播_CCTV节目官网_央视网​ 来源:cctv8从来没有那个电视节目比这个卡的。无论如何:我会一直追剧下去的。......
  • java CountDownLatch用法 主线程等待子线程执行完后再执行
    这里记录一下下面这种情况:主线程需要等待多个子线程执行完后再执行。我们先看一下下面的场景:packagecom.java4all.mypoint;importjava.util.concurrent.CountDownLatch;/*......
  • CountDownLatch和CyclicBarrier的使用
    CountDownLatch和CyclicBarrier都可以用来让一个线程等待其他线程执行完再继续执行一、CountDownLatchpublicclassTest8{staticLoggerLOG=LoggerFactory.ge......
  • CountDownLatch和FutureTask类使用方法解析
    摘要:使用CountDownLatch和FutureTask解决主线程需要拿到多个子线程任务的执行结果之后再进行执行的问题。综述  我们在工作中,经常遇到有些业务场景需要使用多线程异步......
  • CountDownLatch简单使用
    如何保证主线程在副线程执行结束后才会执行结束,这里使用CountDownLatch    //设置三个线程需要执行CountDownLatchlatch=newCountDownLatch(3);......
  • 三体数据爬取
    《三体》动漫点评区数据爬取预备##准备的库importpandasaspd#数据分析库importrequests#用于发送HTTP请求importjson#json格式importnumpyasnp......
  • CountDownLatch和CyclicBarrier 专题
     4、Runnable接口和Callable接口的区别有点深的问题了,也看出一个Java程序员学习知识的广度。Runnable接口中的run()方法的返回值是void,它做的事情只是纯粹地去执行run()方......
  • JUC同步工具CountDownLatch
    CountDownLatch:允许一条或多条线程等待其它线程中的一组操作完成后再继续执行。 在探究CountDownLatch之前,我们知道Thread的join也有类似功能,先看thread的join方法:1......