一、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 
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_(
),

          threads_(numThreads)

    {

        
for (
int i = 
; i < numThreads; ++i)

        {

            
char name[
];

            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(
);

    sleep(
);

    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 = 
; i < numThreads; ++i)

        {

            
char name[
];

            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(
);

       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(
);

    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类封装中的知识点的更多相关文章

  1. muduo网络库学习笔记(四) 通过eventfd实现的事件通知机制

    目录 muduo网络库学习笔记(四) 通过eventfd实现的事件通知机制 eventfd的使用 eventfd系统函数 使用示例 EventLoop对eventfd的封装 工作时序 runInLoo ...

  2. muduo网络库学习笔记(五) 链接器Connector与监听器Acceptor

    目录 muduo网络库学习笔记(五) 链接器Connector与监听器Acceptor Connector 系统函数connect 处理非阻塞connect的步骤: Connetor时序图 Accep ...

  3. muduo网络库学习笔记(三)TimerQueue定时器队列

    目录 muduo网络库学习笔记(三)TimerQueue定时器队列 Linux中的时间函数 timerfd简单使用介绍 timerfd示例 muduo中对timerfd的封装 TimerQueue的结 ...

  4. muduo 网络库学习之路(一)

    前提介绍: 本人是一名大三学生,主要使用C++开发,兴趣是高性能的服务器方面. 网络开发离不开网络库,所以今天开始学一个新的网络库,陈老师的muduo库 我参考的书籍就是陈老师自己关于muduo而编著 ...

  5. muduo网络库学习笔记(10):定时器的实现

    传统的Reactor通过控制select和poll的等待时间来实现定时,而现在在Linux中有了timerfd,我们可以用和处理IO事件相同的方式来处理定时,代码的一致性更好. 一.为什么选择time ...

  6. muduo网络库架构总结

    目录 muduo网络库简介 muduo网络库模块组成 Recator反应器 EventLoop的两个组件 TimerQueue定时器 Eventfd Connector和Acceptor连接器和监听器 ...

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

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

  8. 长文梳理muduo网络库核心代码、剖析优秀编程细节

    前言 muduo库是陈硕个人开发的tcp网络编程库,支持Reactor模型,推荐大家阅读陈硕写的<Linux多线程服务端编程:使用muduo C++网络库>.本人前段时间出于个人学习.找工 ...

  9. muduo网络库使用心得

    上个月看了朋友推荐的mudo网络库,下完代码得知是国内同行的开源作品,甚是敬佩.下了mudo使用手冊和035版的代码看了下结构,感觉是一个比較成熟并且方便使用的网络库.本人手头也有自己的网络库,尽管不 ...

随机推荐

  1. Python内置类型性能分析

    Python内置类型性能分析 timeit模块 timeit模块可以用来测试一小段Python代码的执行速度. class timeit.Timer(stmt='pass', setup='pass' ...

  2. 大型运输行业实战_day03_1_基于intellij idea的非maven spring+springMVC+mybatis搭建

    1.搭建标准web项目结构 搭建完成后的项目结构如图 1.创建普通web项目(略) 2.在lib中添加jar包 3.在resources中添加spring-config.xml主配置文件 <?x ...

  3. Express 应用生成器

    [Express 应用生成器] 通过应用生成器工具 express 可以快速创建一个应用的骨架. 通过如下命令安装,-g意味着安装在全局目录下: 下面的示例就是在当前工作目录下创建一个命名为 myap ...

  4. Our Journey of Xian Ends

    Our Journey of Xian Ends https://nanti.jisuanke.com/t/18521 262144K   Life is a journey, and the roa ...

  5. 使用HttpModule实现网址重写和HttpHandler实现页面静态化冲突的解决办法

    使用HttpModule实现网址重写和HttpHandler冲突的解决办法功能描述:1. 用HttpModule做了一个重写URL的功能,实现所有访问html的请求要经过httpModule处理,如果 ...

  6. tcp连接需要注意的问题

    当有子进程时,子进程终止时会返回SIGCHLD信号,默认忽略,此时会有僵尸进程. 处理方法: 捕获信号,并waitpid. 当慢系统调用被中断时(如信号中断),有些系统不会自动重启调用,此时系统调用可 ...

  7. 获取客户端真实IP地址

    Java-Web获取客户端真实IP: 发生的场景:服务器端接收客户端请求的时候,一般需要进行签名验证,客户端IP限定等情况,在进行客户端IP限定的时候,需要首先获取该真实的IP. 一般分为两种情况: ...

  8. 关于mybatis缓存配置详解

    一级缓存: 一级缓存是默认的. 测试:在WEB页面同一个查询执行两次从日志里面看同样的sql查询执行两次. 2次sql查询,看似我们使用了同一个sqlSession,但是实际上因为我们的dao继承了S ...

  9. javascript正则表达式验证密码(必须含数字字符特殊符号,长度4-16位之间)

    var newpwd = $("#newpassword").val(); //var pattern = "([A-Za-z]|[0-9]|-|_){4,16}&quo ...

  10. 利用ks构建ISO中的一些坑

    构建ISO的基本流程 1.获取rpm包源码 2.将源码增量编译成二进制包 3.编写ks的包列表决定ISO制作时需要从什么地方(二进制仓库repo)取哪些二进制包 4.通过createiso命令并指定k ...