• 多线程消息处理


    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. }

  • 相关阅读:
    C++ Qt中其他类调用窗口中的ui控件
    React 组件传 children 的各种方案
    教科书级别 IDEA中用Maven手工搭建Spring Boot项目
    Unity vscode 官方debug
    重读百度移动生态:“第一曲线”的创新“延长线”
    NFT 推荐|史蒂夫·青木 NFT 作品集
    阿里云ACP考试内容是什么?考试时间是什么时候?
    JavaScript学习(五)——首页跳转实现
    Spring Boot中使用Redis进行大数据缓存
    算法训练.
  • 原文地址:https://blog.csdn.net/zhaodongdong2012/article/details/134483947