muduo網路庫學習之MutexLock類、MutexLockGuard類、Condition類、CountDownLatch類封裝中的知識點
一、MutexLock 類

二、MutexLockGuard類
三、Condition類
四、CountDownLatch類

也可以用於主執行緒等待子執行緒初始化完畢才開始工作
下面寫兩個程式測試一下CountDownLatch 的作用:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
#include <muduo/base/CountDownLatch.h>
#include <muduo/base/Thread.h> #include <boost/bind.hpp> #include <boost/ptr_container/ptr_vector.hpp> #include <string> #include <stdio.h> using namespace muduo; class Test { public: Test(int numThreads) : latch_(1), threads_(numThreads) { for (int i = 0; i < numThreads; ++i) { char name[32]; snprintf(name, sizeof name, "work thread %d", i); threads_.push_back(new muduo::Thread( boost::bind(&Test::threadFunc, this), muduo::string(name))); } for_each(threads_.begin(), threads_.end(), boost::bind(&Thread::start, _1)); } void run() { latch_.countDown(); } void joinAll() { for_each(threads_.begin(), threads_.end(), boost::bind(&Thread::join, _1)); } private: void threadFunc() { latch_.wait(); printf("tid=%d, %s started\n", CurrentThread::tid(), CurrentThread::name()); printf("tid=%d, %s stopped\n", CurrentThread::tid(), CurrentThread::name()); } CountDownLatch latch_; boost::ptr_vector<Thread> threads_; }; int main() { printf("pid=%d, tid=%d\n", ::getpid(), CurrentThread::tid()); Test t(3); sleep(3); printf("pid=%d, tid=%d %s running ...\n", ::getpid(), CurrentThread::tid(), CurrentThread::name()); t.run(); t.joinAll(); printf("number of created threads %d\n", Thread::numCreated()); } |
simba@ubuntu:~/Documents/build/debug/bin$ ./countdownlatch_test1
pid=2994, tid=2994
pid=2994, tid=2994 main running ...
tid=2997, work thread 2 started
tid=2997, work thread 2 stopped
tid=2996, work thread 1 started
tid=2996, work thread 1 stopped
tid=2995, work thread 0 started
tid=2995, work thread 0 stopped
number of created threads 3
simba@ubuntu:~/Documents/build/debug/bin$
可以看到其他三個執行緒一直等到主執行緒睡眠完執行run(),在裡面執行latch_.countDown() 將計數減為0,進而執行notifyall 喚醒後,才開始執行下來。
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
#include <muduo/base/CountDownLatch.h>
#include <muduo/base/Thread.h> #include <boost/bind.hpp> #include <boost/ptr_container/ptr_vector.hpp> #include <string> #include <stdio.h> using namespace muduo; class Test { public: Test(int numThreads) : latch_(numThreads), threads_(numThreads) { for (int i = 0; i < numThreads; ++i) { char name[32]; snprintf(name, sizeof name, "work thread %d", i); threads_.push_back(new muduo::Thread( boost::bind(&Test::threadFunc, this), muduo::string(name))); } for_each(threads_.begin(), threads_.end(), boost::bind(&muduo::Thread::start, _1)); } void wait() { latch_.wait(); } void joinAll() { for_each(threads_.begin(), threads_.end(), boost::bind(&Thread::join, _1)); } private: void threadFunc() { sleep(3); printf("tid=%d, %s started\n",
CurrentThread::tid(), CurrentThread::name()); latch_.countDown(); printf("tid=%d, %s stopped\n", CurrentThread::tid(), CurrentThread::name()); } CountDownLatch latch_; boost::ptr_vector<Thread> threads_; }; int main() { printf("pid=%d, tid=%d\n", ::getpid(), CurrentThread::tid()); Test t(3); t.wait(); printf("pid=%d, tid=%d %s running ...\n", ::getpid(), CurrentThread::tid(), CurrentThread::name()); t.joinAll(); printf("number of created threads %d\n", Thread::numCreated()); } |
simba@ubuntu:~/Documents/build/debug/bin$ ./countdownlatch_test2
pid=4488, tid=4488
tid=4491, work thread 2 started
tid=4491, work thread 2 stopped
tid=4490, work thread 1 started
tid=4490, work thread 1 stopped
tid=4489, work thread 0 started
pid=4488, tid=4488 main running ...
tid=4489, work thread 0 stopped
number of created threads 3
可以看出當其他三個執行緒都啟動後,各自執行一次 latch_.countDown(),主執行緒wait() 返回繼續執行下去。
相關文章
- muduo網路庫Timestamp類
- muduo網路庫Exception異常類Exception
- muduo網路庫AtomicIntegerT原子整數類
- 靜態庫封裝之ComStr類封裝
- 靜態庫封裝之ComFile類封裝
- 靜態庫封裝之ComDir類封裝
- Laravel 小知識點之 HtmlString 類LaravelHTML
- Android之Activity基類封裝Android封裝
- 同步工具類—— CountDownLatchCountDownLatch
- VR垃圾分類體驗系統:學習垃圾分類知識科普VR
- 一、類的封裝性封裝
- 封裝Date工具類封裝
- 封裝Redis工具類封裝Redis
- educoder上的實訓題目(學習-Java包裝類之Byte類)Java
- 【入門知識】網路安全中的漏洞分為哪幾類?
- Retrofit+okhttp+Rxjava封裝網路請求工具類HTTPRxJava封裝
- 十五、類與封裝的概念封裝
- JavaSE基礎知識學習—–抽象類和介面Java抽象
- c#封裝DBHelper類C#封裝
- 4、類和物件—封裝物件封裝
- 自用驗證類封裝封裝
- Java類和物件知識點總結Java物件
- JAVA類庫之——Character類(持續更新)Java
- muduo原始碼解析11-logger類原始碼
- C#學習——基本類——Math類C#
- Python 學習筆記之類「物件導向,超類,抽象」Python筆記物件抽象
- JAVA集合類簡要筆記 - 內部類 包裝類 Object類 String類 BigDecimal類 system類Java筆記ObjectDecimal
- c# Lambda操作類封裝C#封裝
- Java多執行緒同步工具類之CountDownLatchJava執行緒CountDownLatch
- Java類的設計和封裝及類成員的訪問控制Java封裝
- 學習javaScript必知必會(6)~類、類的定義、prototype 原型、json物件JavaScript原型JSON物件
- 包裝類共同點
- 如何講清楚 Java 物件導向的問題與知識?(類與物件,封裝,繼承,多型,介面,內部類...)Java物件封裝繼承多型
- C/C++ Zlib庫封裝MyZip壓縮類C++封裝
- 使用C++/CLI呼叫C#封裝類庫C++C#封裝
- MFC之動態呼叫自己寫的類庫中的類的成員方法
- java學習:Java中的其它類Java
- 通過Handler封裝的網路請求資料的工具類NetUtils封裝
- 【Django drf】檢視類APIView之五層封裝 ApiView的類屬性 drf配置檔案DjangoAPIView封裝