muduo里的CountDownLatch类实际上是对条件变量condition进行的封装,既可以用于所有子线程等待主线程发起 “起跑” ,也可以用于主线程等待子线程初始化完毕才开始工作。

condition.h代码如下:

// Use of this source code is governed by a BSD-style license
// that can be found in the License file.
//
// Author: Shuo Chen (chenshuo at chenshuo dot com)
//条件变量
#ifndef MUDUO_BASE_CONDITION_H
#define MUDUO_BASE_CONDITION_H #include <muduo/base/Mutex.h> #include <boost/noncopyable.hpp>
#include <pthread.h> namespace muduo
{ class Condition : boost::noncopyable
{
public:
//构造函数只能显式调用
explicit Condition(MutexLock& mutex) : mutex_(mutex)
{//构造函数初始化条件变量
pthread_cond_init(&pcond_, NULL);
}
//析构函数
~Condition()
{//析构函数销毁条件变量
pthread_cond_destroy(&pcond_);
}
//等待函数
void wait()
{
pthread_cond_wait(&pcond_, mutex_.getPthreadMutex());
} // returns true if time out, false otherwise.
bool waitForSeconds(int seconds);
//signal函数
void notify()
{
pthread_cond_signal(&pcond_);
}
//broadcast函数
void notifyAll()
{
pthread_cond_broadcast(&pcond_);
} private:
MutexLock& mutex_;//锁,不拥有他,是一个引用,不负责管理他的生存期
pthread_cond_t pcond_;//为一个条件变量
}; }
#endif // MUDUO_BASE_CONDITION_H

condition.cc

// Use of this source code is governed by a BSD-style license
// that can be found in the License file.
//
// Author: Shuo Chen (chenshuo at chenshuo dot com) #include <muduo/base/Condition.h> #include <errno.h> // returns true if time out, false otherwise.
bool muduo::Condition::waitForSeconds(int seconds)
{
struct timespec abstime;
clock_gettime(CLOCK_REALTIME, &abstime);
abstime.tv_sec += seconds;
return ETIMEDOUT == pthread_cond_timedwait(&pcond_, mutex_.getPthreadMutex(), &abstime);
}

CountDownLatch.h

// Use of this source code is governed by a BSD-style license
// that can be found in the License file.
//
// Author: Shuo Chen (chenshuo at chenshuo dot com)
//对条件变量的封装,既可以用于所有子线程等待主线程发起起跑命令,
//也可以用于主线程等待子线程初始化完毕才开始工作
#ifndef MUDUO_BASE_COUNTDOWNLATCH_H
#define MUDUO_BASE_COUNTDOWNLATCH_H #include <muduo/base/Condition.h>
#include <muduo/base/Mutex.h> #include <boost/noncopyable.hpp> namespace muduo
{ class CountDownLatch : boost::noncopyable
{
public:
//构造函数,显示调用
explicit CountDownLatch(int count);
//等待函数
void wait();
//计数器减
void countDown();
//获取当前计数器的值
int getCount() const; private://mutable表明在const里可以改变他的状态
mutable MutexLock mutex_;//互斥锁
Condition condition_;//条件变量
int count_;//计数器
}; }
#endif // MUDUO_BASE_COUNTDOWNLATCH_H

CountDownLatch.cc

// Use of this source code is governed by a BSD-style license
// that can be found in the License file.
//
// Author: Shuo Chen (chenshuo at chenshuo dot com) #include <muduo/base/CountDownLatch.h> using namespace muduo;
//构造函数对值进行初始化,参数为计数器,构造mutex_对象,将mutex_对象传到condition_里面
CountDownLatch::CountDownLatch(int count): mutex_(), condition_(mutex_),count_(count)
{
} void CountDownLatch::wait()
{
MutexLockGuard lock(mutex_);
//count_不为0则一直等待
while (count_ > 0)
{
condition_.wait();
}
} void CountDownLatch::countDown()
{
MutexLockGuard lock(mutex_);
--count_;//count_减少
if (count_ == 0)
{//如果count_为0,则通知所有的等待线程
condition_.notifyAll();
}
} int CountDownLatch::getCount() const
{//得到count_的值
MutexLockGuard lock(mutex_);
return count_;
}

测试代码需要自己编写,如下所示,该程序先打印主进程id,建立3个线程,wait等待主线程发号施令,然后睡眠3秒,之后发起号令,打印各个线程的id,代码如下:

#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)
{//CountDownLatch对象的计数值初始化为1,线程对象数组的容量初始化为传进来的参数
for (int i = 0; i < numThreads; ++i)
{//创建numThreads个线程
char name[32];//线程的名称
snprintf(name, sizeof name, "work thread %d", i);
//创建线程,threadFunc为回调函数,因为是成员函数,所以要用&,this为当前类指针
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()
{//count_初始化的时候赋为1,这里只需执行一次既可以跳出等待
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_;//CountDownLatch对象
boost::ptr_vector<Thread> threads_;//线程对象数组
}; int main()
{//首先打印当前进程pid,当前线程tid
printf("pid=%d, tid=%d\n", ::getpid(), CurrentThread::tid());
//构造Test对象
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());
}

由于线程竞争,运行结果不唯一:



另一个结果:

muduo网络库源码学习————条件变量的更多相关文章

  1. muduo网络库源码学习————Timestamp.cc

    今天开始学习陈硕先生的muduo网络库,moduo网络库得到很多好评,陈硕先生自己也说核心代码不超过5000行,所以我觉得有必要拿过来好好学习下,学习的时候在源码上面添加一些自己的注释,方便日后理解, ...

  2. muduo网络库源码学习————线程池实现

    muduo库里面的线程池是固定线程池,即创建的线程池里面的线程个数是一定的,不是动态的.线程池里面一般要包含线程队列还有任务队列,外部程序将任务存放到线程池的任务队列中,线程池中的线程队列执行任务,也 ...

  3. muduo网络库源码学习————无界队列和有界队列

    muduo库里实现了两个队列模板类:无界队列为BlockingQueue.h,有界队列为BoundedBlockingQueue.h,两个测试程序实现了生产者和消费者模型.(这里以无界队列为例,有界队 ...

  4. muduo网络库源码学习————互斥锁

    muduo源码的互斥锁源码位于muduo/base,Mutex.h,进行了两个类的封装,在实际的使用中更常使用MutexLockGuard类,因为该类可以在析构函数中自动解锁,避免了某些情况忘记解锁. ...

  5. muduo网络库源码学习————线程类

    muduo库里面的线程类是使用基于对象的编程思想,源码目录为muduo/base,如下所示: 线程类头文件: // Use of this source code is governed by a B ...

  6. muduo网络库源码学习————原子性操作Atomic.h

    原子性操作可以做到比互斥锁更小的开销,在多线程编程中原子性操作是非常有用的.Atomic.h文件位于muduo/base下,代码如下: // Use of this source code is go ...

  7. muduo网络库源码学习————日志滚动

    muduo库里面的实现日志滚动有两种条件,一种是日志文件大小达到预设值,另一种是时间到达超过当天.滚动日志类的文件是LogFile.cc ,LogFile.h 代码如下: LogFile.cc #in ...

  8. muduo网络库源码学习————日志类封装

    muduo库里面的日志使方法如下 这里定义了一个宏 #define LOG_INFO if (muduo::Logger::logLevel() <= muduo::Logger::INFO) ...

  9. muduo网络库源码学习————线程特定数据

    muduo库线程特定数据源码文件为ThreadLocal.h //线程本地存储 // Use of this source code is governed by a BSD-style licens ...

随机推荐

  1. NS网络仿真,小白起步版,模拟仿真之间注意的事项

    FTP是基于TCP的,所以FTP应用不可以绑定UDP发送代理 FTP和CBR属于应用流,他们用来绑定TCP和UDP发送代理 TCP用于发送代理时,接收代理为TCPSink,可以绑定FTP应用.CBR流 ...

  2. MODIS系列之NDVI(MOD13Q1)一:数据下载(一)基于插件

    引言: 写MODIS数据处理这个系列文章的初衷,主要是为了分享本人处理MODIS数据方面的一些经验.鉴于网上对这方面系统性的总结还比较少,我搜集资料时也是走了许多的弯路,因此希望通过此文让初学者能够更 ...

  3. Python爬虫系列(五):分析HTML结构

    今晚,被烦死了.9点多才下班,就想回来看书学习,结果被唠叨唠叨个小时,我不断喊不要和我聊天了,还反复说.我只想安安静静看看书,学习学习,全世界都不要打扰我 接着上一个讨论,我们今晚要分析HTML结构了 ...

  4. Python Requests-学习笔记(2)

    你也许经常想为URL的查询字符串(query string)传递某种数据.如果你是手工构建URL, 那么数据会以键/值 对的形式置于URL中,跟在一个问号的后面.例如,httpbin.org/get? ...

  5. springboot https证书配置

    如果公司有提供证书如: 拿到证书秘钥可直接在springboot 的配置文件中配置: server.ssl.key-store=classpath:cert.pfx server.ssl.key-st ...

  6. flex实现三列布局

    css3新引入的flex在某些情况下布局非常实用 因为它是弹性盒子所以自适应效果会很棒 不过各项布局方案还是各有优劣 <!DOCTYPE html> <html lang=" ...

  7. Threejs【坐标转换】如何让annotation跟随物体一起旋转

    现在根据鼠标点击的屏幕位置能够得到屏幕的坐标event.clientX和event.clientY,然后我的annotation就初始化在这个屏幕坐标的位置,那么如何绑定annotation和三维物体 ...

  8. 植物大战僵尸的代码如何使用python来实现

    前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者:程序IT圈 PS:如有需要Python学习资料的小伙伴可以加点击下方链 ...

  9. 浏览器远程编写python代码--jupyter web server

    公司分配了开发机,偶尔需要写一些python自动化脚本.为了提高编写效率,可以开发机上起一个jupyter web server,然后在电脑chrome浏览器进行编辑. 以下步骤均在开发机上操作. 安 ...

  10. Vulnhub-dpwwn-01靶机过关记录

    靶机地址:172.16.1.192 Kali 目录扫描 查看info.php 端口扫描 开放3306,尝试弱密码或爆破mysql. 账户为root,密码为空,成功登陆. 查看数据库:再查看ssh表 查 ...