zoukankan      html  css  js  c++  java
  • C++多线程chap2多线程通信和同步7_一个例子

    这里,只是记录自己的学习笔记。

    顺便和大家分享多线程的基础知识。然后从入门到实战。有代码。

    知识点来源:

    https://edu.51cto.com/course/26869.html



    使用互斥锁 + list 模拟 线程通信

    • 1.封装线程基类 XThread 控制线程启动和停止
    • 2.模拟消息服务器线程,接收字符串消息,并模拟处理
    • 3.通过 unique_lock 和 mutex 互斥访问 list<string> 消息队列
    • 4.主线程定时发送消息给子线程

    XThread 线程基类

    xthread.h  文件 

     1 #pragma once
     2 #include <thread>
     3 
     4 class XThread
     5 {
     6 public:
     7     //启动线程
     8     virtual void Start();
     9 
    10     //设置线程退出标志  并等待
    11     virtual void Stop();
    12 
    13     //等待线程退出(阻塞)
    14     virtual void Wait();
    15 
    16     //线程是否退出
    17     bool is_exit();
    18 
    19 private:
    20     //线程入口
    21     virtual void Main() = 0;
    22     bool is_exit_ = false;
    23     std::thread th_;
    24 };

    xthread.cpp  文件

     1 #include "xthread.h"
     2 
     3 using namespace std;
     4 
     5 
     6 //启动线程
     7 void XThread::Start() {
     8     is_exit_ = false;
     9     th_ = thread(&XThread::Main, this);
    10 }
    11 
    12 //设置线程退出标志  并等待
    13 void XThread::Stop() {
    14     is_exit_ = true;
    15     Wait();
    16 }
    17 
    18 //等待线程退出(阻塞)
    19 void XThread::Wait() {
    20     if (th_.joinable()) {
    21         th_.join();
    22     }
    23 }
    24 
    25 //线程是否退出
    26 bool XThread::is_exit() {
    27     return is_exit_;
    28 }

    XMsgServer 消息基类

    xmsg_server.h 文件

     1 #pragma once
     2 #include "xthread.h"
     3 #include <string>
     4 #include <list>
     5 #include <mutex>
     6 class XMsgServer:public XThread
     7 {
     8 public:
     9     //给当前线程发消息
    10     void SendMsg(std::string msg);
    11 
    12 private:
    13     //处理消息的线程入口函数
    14     void Main()  override;
    15 
    16     //可以考虑给消息队列设置一个最大值,超过最大值就阻塞,或者发送失败等等
    17     //消息队列缓冲
    18     std::list<std::string> msgs_;
    19 
    20     //互斥访问消息队列
    21     std::mutex mux_;
    22 };

    xmsg_server.cpp 文件

     1 #include "xmsg_server.h"
     2 #include <iostream>
     3 using namespace std;
     4 using namespace this_thread;
     5 
     6 //给当前线程发消息
     7 void XMsgServer::SendMsg(std::string msg) {
     8     unique_lock<mutex> lock(mux_);
     9     msgs_.push_back(msg);
    10 }
    11 
    12 
    13     //处理消息的线程入口函数
    14 void XMsgServer::Main() {
    15     while(!is_exit()) {
    16         sleep_for(10ms);
    17         unique_lock<mutex> lock(mux_);
    18         if (msgs_.empty())
    19             continue;
    20         while (!msgs_.empty()) {
    21             //消息处理业务逻辑
    22             cout << "recv : " << msgs_.front() << endl;
    23             msgs_.pop_front();
    24         }
    25     }
    26 }

    hello.cpp文件

     1 #include <iostream>
     2 #include <string>
     3 #include <mutex>
     4 #include <thread>
     5 #include "xmsg_server.h"
     6 #include <sstream>
     7 using namespace std;
     8 
     9 int main()
    10 {
    11     XMsgServer  server;
    12     server.Start();
    13     for (int i = 0; i < 10; i++) {
    14         stringstream ss;
    15         ss << " msg : " << i + 1;
    16         server.SendMsg(ss.str());
    17         this_thread::sleep_for(500ms);
    18     }
    19 
    20     server.Stop();
    21     return 0;
    22 }

     

     

    作者:小乌龟
    【转载请注明出处,欢迎转载】 希望这篇文章能帮到你

     

  • 相关阅读:
    Redis集群持久化
    Redis集群介绍
    电脑CPU讲解
    Centos7 k8s v1.5.2二进制部署安装-交付jenkins到k8s集群
    Centos7 k8s v1.5.2二进制部署安装-交付dubbo服务到k8s集群准备工作
    华为云全栈产品福利来袭,终身VIP免费送!
    Centos7 k8s v1.5.2二进制部署安装-dashboard--WEB管理
    Centos7 k8s v1.5.2二进制部署安装-服务暴露ingress控制器之traefik
    简单两步禁止 iOS 系统频繁提示更新,亲测有效
    Linux常用命令
  • 原文地址:https://www.cnblogs.com/music-liang/p/15597853.html
Copyright © 2011-2022 走看看