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網路庫學習之BlockinngQueue類、ThreadPool 類、Singleton類封裝中的知識點BloCthread封裝
- muduo網路庫學習之Timestamp類、AtomicIntegerT 類封裝中的知識點封裝
- muduo網路庫學習之Logger類、LogStream類、LogFile類封裝中的知識點封裝
- muduo網路庫學習之ThreadLocal 類、ThreadLocalSingleton類封裝知識點thread封裝
- muduo網路庫學習之Exception類、Thread 類封裝中的知識點(重點講pthread_atfork())Exceptionthread封裝
- muduo網路庫學習之EventLoop(四):EventLoopThread 類、EventLoopThreadPool 類OOPthread
- muduo網路庫學習筆記(8):高效日誌類的封裝筆記封裝
- muduo網路庫學習之muduo_http 庫涉及到的類HTTP
- muduo網路庫學習之muduo_inspect 庫涉及到的類
- muduo網路庫Timestamp類
- muduo網路庫學習筆記(1):Timestamp類筆記
- muduo網路庫學習筆記(3):Thread類筆記thread
- muduo網路庫Exception異常類Exception
- 13封裝網路請求類庫封裝
- muduo網路庫學習筆記(12):TcpServer和TcpConnection類筆記TCPServer
- muduo網路庫AtomicIntegerT原子整數類
- muduo網路庫學習筆記(6):單例類(執行緒安全的)筆記單例執行緒
- muduo網路庫學習之EventLoop(一):事件迴圈類圖簡介和muduo 定時器TimeQueueOOP事件定時器
- java學習之Date類、DateFormat類JavaORM
- Laravel 小知識點之 HtmlString 類LaravelHTML
- Java中類的一些知識點Java
- java學習 -- 利用類的反射和泛型自己動手寫jdbc封裝小類庫Java反射泛型JDBC封裝
- JavaSE基礎知識學習-----Object類JavaObject
- 封裝xunsearch類封裝
- JS 封裝類JS封裝
- 同步工具類—— CountDownLatchCountDownLatch
- VR垃圾分類體驗系統:學習垃圾分類知識科普VR
- java學習之BigInteger類、BigDecimal類JavaDecimal
- java學習之基本包裝類Java
- Java學習--Java 中的包裝類Java
- php的curl封裝類PHP封裝
- 封裝Date工具類封裝
- JsonValue 封裝類JSON封裝
- Android之Activity基類封裝Android封裝
- 物件與類_知識點筆記物件筆記
- Stella 知識庫--模型類的設計模型
- C++中類相關知識點總結C++
- JavaSE基礎知識學習—–抽象類和介面Java抽象