码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 多线程消息处理


    MultiThread.h:

    1. #pragma once
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. using TaskFun = std::function<void(void)>;
    9. class CMultiThread {
    10. public:
    11. CMultiThread();
    12. ~CMultiThread();
    13. bool start();
    14. void stop();
    15. bool post(const TaskFun& task_fun);
    16. private:
    17. void exec();
    18. private:
    19. bool exist_;
    20. std::list> threads_;
    21. std::queue tasks_;
    22. std::mutex mutex_;
    23. std::condition_variable condition_;
    24. int32_t thead_num_;
    25. };

    MultiThread.cpp:

    1. #include "MultiThread.h"
    2. #include
    3. CMultiThread::CMultiThread()
    4. : exist_(false)
    5. , thead_num_(4)
    6. {
    7. }
    8. CMultiThread::~CMultiThread() {}
    9. bool CMultiThread::start() {
    10. if (exist_) {
    11. return false;
    12. }
    13. exist_ = true;
    14. for (size_t i = 0; i < thead_num_; i++)
    15. {
    16. std::shared_ptr sptr_thread =
    17. std::make_shared(std::thread(std::bind(&CMultiThread::exec, this)));
    18. threads_.push_back(sptr_thread);
    19. }
    20. return true;
    21. }
    22. void CMultiThread::stop() {
    23. if (!exist_) {
    24. return;
    25. }
    26. exist_ = false;
    27. condition_.notify_all();
    28. for (auto& iter_thread : threads_)
    29. {
    30. if (iter_thread->joinable()) {
    31. iter_thread->join();
    32. }
    33. }
    34. return;
    35. }
    36. bool CMultiThread::post(const TaskFun& task_fun) {
    37. std::lock_guard lock(mutex_);
    38. tasks_.push(task_fun);
    39. condition_.notify_one();
    40. return true;
    41. }
    42. void CMultiThread::exec() {
    43. while (exist_)
    44. {
    45. TaskFun task = nullptr;
    46. {
    47. std::unique_lock lock(mutex_);
    48. if (tasks_.empty()) {
    49. condition_.wait(lock);
    50. }
    51. if (!exist_) {
    52. break;
    53. }
    54. if (!tasks_.empty())
    55. {
    56. task = tasks_.front();
    57. tasks_.pop();
    58. }
    59. }
    60. if (nullptr != task) {
    61. task();
    62. }
    63. }
    64. return;
    65. }

    main:

    1. #include
    2. #include "MultiThread.h"
    3. int main() {
    4. std::mutex m;
    5. CMultiThread t;
    6. t.start();
    7. auto fun = [&]()
    8. {
    9. std::lock_guard lock(m);
    10. static int count = 0;
    11. std::cout << "Count: " << ++count << "thread-id: " << std::this_thread::get_id() << std::endl;
    12. std::this_thread::sleep_for(std::chrono::seconds(1));
    13. };
    14. for (int i = 0; i < 20; ++i)
    15. {
    16. t.post(fun);
    17. //std::this_thread::sleep_for(std::chrono::microseconds(500));
    18. }
    19. getchar();
    20. t.post(fun);
    21. getchar();
    22. t.stop();
    23. return 0;
    24. }

  • 相关阅读:
    获取spring容器中的bean实例
    Golang Linux 安装与环境变量配置
    C++中float和double的比较
    Spring设计模式,事务管理和代理模式的应用
    《SQL Server基础——SQL语句》
    (4)通过调用hadoop的java api实现本地文件上传到hadoop文件系统上
    如何避免旧请求的数据覆盖掉最新请求
    计组 | 各程序员、用户 可见 / 不可见 寄存器总结
    win11怎么关闭触控板?win11关闭触控板的三种解决方法
    Matlab:在不同区域设置之间共享代码和数据
  • 原文地址:https://blog.csdn.net/zhaodongdong2012/article/details/134483947
  • 最新文章
  • 【JVM】编译执行与解释执行的区别是什么?JVM 使用哪种方式?
    用 Hashids 优雅解决 C 端自增 ID 暴露问题
    V8引擎 精品漫游指南--Ignition篇(上) 指令 栈帧 槽位 调用约定 内存布局 基础内容
    LLVM Pass快速入门(四):代码插桩
    milkup:桌面端 markdown AI续写和即时渲染
    基于项目工程构建SBOM(软件物料清单)的研究
    鸿蒙应用开发UI基础第二节:鸿蒙应用程序框架核心解析与实操
    .NET 中如何快速实现 List 集合去重?
    扣子Coze实战:从0到1打造抖音+小红书热点监控智能体
    浅谈数据访问层
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号