muduo网络库学习之MutexLock类、MutexLockGuard类、Condition类、CountDownLatch类封装中的知识点
2013-10-31 19:47
381 查看
一、MutexLock 类
class MutexLock : boost::noncopyable
二、MutexLockGuard类
class MutexLockGuard : boost::noncopyable
三、Condition类
class Condition : boost::noncopyable
某个线程:
加锁
while (条件)
wait(); //1、解锁;2、等待通知;3、得到通知返回前重新加锁
解锁
另一个线程:
加锁
更改条件
通知notify(可以移到锁外)
解锁
四、CountDownLatch类
class CountDownLatch : boost::noncopyable
既可以用于所有子线程等待主线程发起 “起跑”
也可以用于主线程等待子线程初始化完毕才开始工作
下面写两个程序测试一下CountDownLatch 的作用:
CountDownLatch_test1:
C++ Code
执行结果如下:
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 唤醒后,才开始执行下来。
CountDownLatch_test2:
C++ Code
执行结果输出如下:
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 manual.pdf
《linux 多线程服务器编程:使用muduo c++网络库》
class MutexLock : boost::noncopyable
二、MutexLockGuard类
class MutexLockGuard : boost::noncopyable
三、Condition类
class Condition : boost::noncopyable
某个线程:
加锁
while (条件)
wait(); //1、解锁;2、等待通知;3、得到通知返回前重新加锁
解锁
另一个线程:
加锁
更改条件
通知notify(可以移到锁外)
解锁
四、CountDownLatch类
class CountDownLatch : boost::noncopyable
既可以用于所有子线程等待主线程发起 “起跑”
也可以用于主线程等待子线程初始化完毕才开始工作
下面写两个程序测试一下CountDownLatch 的作用:
CountDownLatch_test1:
C++ Code
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 唤醒后,才开始执行下来。
CountDownLatch_test2:
C++ Code
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 manual.pdf
《linux 多线程服务器编程:使用muduo c++网络库》
相关文章推荐
- muduo网络库学习之MutexLock类、MutexLockGuard类、Condition类、CountDownLatch类封装中的知识点
- muduo 5 网络库学习之MutexLock类、MutexLockGuard类、Condition类、CountDownLatch类封装中的知识点
- C#中的继承封装和多态知识点总结
- CountDownLatch类介绍
- muduo网络库学习之Exception类、Thread 类封装中的知识点(重点讲pthread_atfork())
- muduo网络库学习之BlockinngQueue<T>类、ThreadPool 类、Singleton类封装中的知识点
- jQuery常用知识点总结以及平时封装常用函数
- JAVA基础知识点梳理七:封装
- jQuery常用知识点总结以及平时封装常用函数
- Android知识点之网络底层封装:细数常用的网络请求框架
- java多线程等待协调工作:CountDownLatch类的高级应用
- muduo网络库学习之ThreadLocal<T> 类、ThreadLocalSingleton<T>类封装知识点
- 知识点3:谈谈封装和抽象的区别
- java多线程之CountDownLatch类基本用法
- JAVA面向对象知识点总结(2)—封装、构造函数、构造代码块、this关键字
- Java核心知识点学习----多线程 倒计时记数器CountDownLatch和数据交换的Exchanger
- muduo 3 网络库学习之Timestamp类、AtomicIntegerT 类封装中的知识点
- C++ 知识点(四):面向对象编程:多态,虚函数,数据抽象,数据封装,抽象类
- 封装+继承+多态-零星知识点
- Core Java第七章知识点总结——封装、继承、多态