Loading [MathJax]/jax/output/CommonHTML/config.js
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >muduo网络库学习之MutexLock类、MutexLockGuard类、Condition类、CountDownLatch类封装中的知识点

muduo网络库学习之MutexLock类、MutexLockGuard类、Condition类、CountDownLatch类封装中的知识点

作者头像
s1mba
发布于 2017-12-28 04:22:55
发布于 2017-12-28 04:22:55
1.2K00
代码可运行
举报
文章被收录于专栏:开发与安全开发与安全
运行总次数:0
代码可运行

一、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:

代码语言:cpp
代码运行次数:0
运行
AI代码解释
复制
#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:

代码语言:cpp
代码运行次数:0
运行
AI代码解释
复制
#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++网络库》

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2013-10-31 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
muduo网络库学习之ThreadLocal<T> 类、ThreadLocalSingleton<T>类封装知识点
该文章介绍了如何使用 muduo C++ 网络库实现多线程服务器。它首先介绍了 muduo 的概况,然后详细讲解了多线程服务器的实现原理和具体代码,最后给出了示例代码和测试结果。
s1mba
2017/12/28
1.3K0
muduo网络库学习之ThreadLocal<T> 类、ThreadLocalSingleton<T>类封装知识点
muduo网络库学习之EventLoop(四):EventLoopThread 类、EventLoopThreadPool 类
1、EventLoopThread(IO线程类) 任何一个线程,只要创建并运行了EventLoop,都称之为IO线程 IO线程不一定是主线程 muduo并发模型one loop per thread + threadpool(计算线程池) 为了方便今后使用,定义了EventLoopThread类,该类封装了IO线程 EventLoopThread创建了一个线程 在线程函数中创建了一个EvenLoop对象并调用EventLoop::loop 多个IO线程可以用IO线程池来管理,对应的类是EventLoo
s1mba
2018/01/15
1.7K0
muduo网络库学习之EventLoop(四):EventLoopThread 类、EventLoopThreadPool 类
muduo网络库学习之Exception类、Thread 类封装中的知识点(重点讲pthread_atfork())
s1mba
2017/12/28
1.3K0
muduo网络库学习之Exception类、Thread 类封装中的知识点(重点讲pthread_atfork())
muduo网络库学习之EventLoop(三):Socket、Acceptor、TcpServer、TcpConnection(连接建立,接收消息)
s1mba
2017/12/28
1.4K0
muduo网络库学习之EventLoop(三):Socket、Acceptor、TcpServer、TcpConnection(连接建立,接收消息)
muduo网络库学习之BlockinngQueue<T>类、ThreadPool 类、Singleton类封装中的知识点
s1mba
2017/12/28
1.1K0
muduo网络库学习之BlockinngQueue<T>类、ThreadPool 类、Singleton类封装中的知识点
muduo网络库学习之Logger类、LogStream类、LogFile类封装中的知识点
s1mba
2018/01/03
1.4K0
muduo网络库学习之Logger类、LogStream类、LogFile类封装中的知识点
缩略muduo库(5):Thread、EventThread、EventThreadPool
文章目录 Thread EventLoopThread EventLoopThreadPool Thread #pragma once #include "nocopyable.hpp" #include <functional> #include <thread> #include <memory> #include <unistd.h> #include <string> #include <atomic> class Thread:public nocpoyable{ public:
看、未来
2021/10/09
3140
muduo网络库学习之EventLoop(一):事件循环类图简介和muduo 定时器TimeQueue
s1mba
2017/12/28
2.1K0
muduo网络库学习之EventLoop(一):事件循环类图简介和muduo 定时器TimeQueue
muduo网络库学习之EventLoop(二):进程(线程)wait/notify 和 EventLoop::runInLoop
s1mba
2017/12/28
1.3K0
muduo网络库学习之EventLoop(二):进程(线程)wait/notify 和 EventLoop::runInLoop
muduo网络库学习之EventLoop(七):TcpClient、Connector
根据给定的文章内容,撰写摘要总结。摘要应该简洁、清晰,并准确反映文章的主要内容。
s1mba
2018/01/03
1.5K0
从实例看muduo网络库各模块交互过程
1、channel 2、Poller 和它的子类 EpollPoller 3、EventLoop 4、Thread、EventLoopThread、EventLoopThreadPool 5、Sock、Acceptor 6、Buffer 7、TcpServer、TCPConnection
看、未来
2021/10/09
4280
从实例看muduo网络库各模块交互过程
muduo网络库学习之EventLoop(六):TcpConnection::send()、shutdown()、handleRead()、handleWrite()
该文章是一篇技术博客,主要介绍了如何利用muduo库实现一个高性能的TCP服务端。文章首先介绍了muduo库的基本概念和特点,然后详细讲解了如何利用muduo库实现一个简单的TCP服务端。最后,文章通过一个实际的例子演示了如何利用muduo库解决实际问题。
s1mba
2017/12/28
1.6K0
muduo网络库学习之EventLoop(六):TcpConnection::send()、shutdown()、handleRead()、handleWrite()
muduo网络库学习之EventLoop(五):TcpConnection生存期管理(连接关闭)
s1mba
2018/01/03
1.5K0
muduo网络库学习之EventLoop(五):TcpConnection生存期管理(连接关闭)
muduo网络库学习之Timestamp类、AtomicIntegerT 类封装中的知识点
s1mba
2017/12/28
8430
muduo网络库学习之Timestamp类、AtomicIntegerT 类封装中的知识点
C++搭建集群聊天室(二):安装muduo网络库
常规操作啦,前面两三篇都是环境搭建。 muduo网络库我就不多做介绍了,一个基于reactor反应堆模型的多线程C++网络库,陈硕大神的作品,不了解的小伙伴可以自行了解一下。
看、未来
2021/09/18
1.5K0
muduo网络库学习之muduo_http 库涉及到的类
s1mba
2018/01/03
2.1K0
muduo网络库学习之muduo_http 库涉及到的类
关于muduo网络库的注解
http://blog.csdn.net/liuxuejiang158blog/article/details/17056537#comments
bear_fish
2018/09/19
8120
【linux学习指南】模拟线程封装与智能指针shared_ptr
std::shared_ptr 是 C++ 标准库 <memory> 头文件中提供的一种智能指针,用于管理动态分配的对象,它实现了共享所有权的语义,下面为你详细介绍它的作用、工作原理以及在你给出的代码中的使用场景。
学习起来吧
2025/02/11
1430
【linux学习指南】模拟线程封装与智能指针shared_ptr
linux网络编程之posix 线程(一):线程模型、pthread 系列函数 和 简单多线程服务器端程序
本文介绍了多线程和线程同步的基础知识,并基于Linux环境进行了详细的实例分析。通过本文的学习,读者可以掌握多线程和线程同步的基本原理,并能够使用相关技术解决实际问题。
s1mba
2017/12/28
3.4K0
linux网络编程之posix 线程(一):线程模型、pthread 系列函数 和 简单多线程服务器端程序
缩略muduo库(4):事件循环 EventLoop
每一个线程都有一个EventLoop,每个loop里面都会有很多的channel,每个channel的任务都要在自己的线程中完成。 为了管理这些线程,设置了一份获取线程ID的代码,辅助管理。
看、未来
2021/10/09
2910
推荐阅读
muduo网络库学习之ThreadLocal<T> 类、ThreadLocalSingleton<T>类封装知识点
1.3K0
muduo网络库学习之EventLoop(四):EventLoopThread 类、EventLoopThreadPool 类
1.7K0
muduo网络库学习之Exception类、Thread 类封装中的知识点(重点讲pthread_atfork())
1.3K0
muduo网络库学习之EventLoop(三):Socket、Acceptor、TcpServer、TcpConnection(连接建立,接收消息)
1.4K0
muduo网络库学习之BlockinngQueue<T>类、ThreadPool 类、Singleton类封装中的知识点
1.1K0
muduo网络库学习之Logger类、LogStream类、LogFile类封装中的知识点
1.4K0
缩略muduo库(5):Thread、EventThread、EventThreadPool
3140
muduo网络库学习之EventLoop(一):事件循环类图简介和muduo 定时器TimeQueue
2.1K0
muduo网络库学习之EventLoop(二):进程(线程)wait/notify 和 EventLoop::runInLoop
1.3K0
muduo网络库学习之EventLoop(七):TcpClient、Connector
1.5K0
从实例看muduo网络库各模块交互过程
4280
muduo网络库学习之EventLoop(六):TcpConnection::send()、shutdown()、handleRead()、handleWrite()
1.6K0
muduo网络库学习之EventLoop(五):TcpConnection生存期管理(连接关闭)
1.5K0
muduo网络库学习之Timestamp类、AtomicIntegerT 类封装中的知识点
8430
C++搭建集群聊天室(二):安装muduo网络库
1.5K0
muduo网络库学习之muduo_http 库涉及到的类
2.1K0
关于muduo网络库的注解
8120
【linux学习指南】模拟线程封装与智能指针shared_ptr
1430
linux网络编程之posix 线程(一):线程模型、pthread 系列函数 和 简单多线程服务器端程序
3.4K0
缩略muduo库(4):事件循环 EventLoop
2910
相关推荐
muduo网络库学习之ThreadLocal<T> 类、ThreadLocalSingleton<T>类封装知识点
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档