• CPP线程管理类实现


     一个线程管理模块应该包含Task(任务类)、Thread(线程类)、线程管理类(ThreadManager)。

    1. #pragma once
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. typedef std::function<void(void*)> ThreadTaskFun;
    10. // 任务类
    11. class Task {
    12. public:
    13. Task(ThreadTaskFun, void* param = nullptr) : func_(std::move(func)), taskParams(param){}
    14. Task() {}
    15. void execute() {
    16. if (func_) {
    17. func_(taskParams);
    18. }
    19. }
    20. private:
    21. std::function<void(void*)> func_;
    22. void* taskParams;
    23. };
    24. // 线程类
    25. class Thread {
    26. public:
    27. Thread() : thread_(), active_(false) {}
    28. void start() {
    29. active_ = true;
    30. thread_ = std::thread([this]() {
    31. while (active_) {
    32. Task task;
    33. {
    34. std::unique_lock lock(mutex_);
    35. // 等待任务队列不为空
    36. condition_.wait(lock, [this]() { return !tasks_.empty() || !active_; });
    37. if (!active_) {
    38. break;
    39. }
    40. task = std::move(tasks_.front());
    41. tasks_.pop();
    42. }
    43. task.execute();
    44. }
    45. });
    46. }
    47. void stop() {
    48. if (active_) {
    49. active_ = false;
    50. condition_.notify_one();
    51. if (thread_.joinable()) {
    52. thread_.join();
    53. }
    54. }
    55. }
    56. void addTask(Task task) {
    57. std::lock_guard lock(mutex_);
    58. tasks_.push(std::move(task));
    59. condition_.notify_one();
    60. }
    61. private:
    62. std::thread thread_;
    63. std::queue tasks_;
    64. std::mutex mutex_;
    65. std::condition_variable condition_;
    66. bool active_;
    67. };
    68. // 线程管理类
    69. class ThreadManager {
    70. public:
    71. ThreadManager(int num_threads) {
    72. for (int i = 0; i < num_threads; ++i) {
    73. threads_.emplace_back(std::make_unique());
    74. }
    75. }
    76. ~ThreadManager()
    77. {
    78. stopThreads();
    79. }
    80. void startThreads() {
    81. for (auto& thread : threads_) {
    82. thread->start();
    83. }
    84. }
    85. void stopThreads() {
    86. for (auto& thread : threads_) {
    87. thread->stop();
    88. }
    89. }
    90. void addTask(Task task) {
    91. if (index_ >= threads_.size()) {
    92. index_ = 0; // 循环使用线程
    93. }
    94. threads_[index_]->addTask(std::move(task));
    95. index_++;
    96. }
    97. private:
    98. std::vector> threads_;
    99. int index_ = 0;
    100. };

    Demo

    1. #include "threadpool.h"
    2. class Resources
    3. {
    4. public:
    5. Resources()
    6. {
    7. resourcesThreadFun = std::bind(&Resources::exampleTaskFunction, this, std::placeholders::_1);
    8. }
    9. void exampleTaskFunction(void* param) {
    10. int index = 0;
    11. std::cout << "Executing task " << index << " in thread: " << std::this_thread::get_id() << std::endl;
    12. }
    13. ThreadTaskFun resourcesThreadFun;
    14. };
    15. int main() {
    16. ThreadManager manager(4); // 创建4个线程
    17. manager.startThreads();
    18. Resources* pResource = new Resources();
    19. // 添加10个示例任务
    20. for (int i = 0; i < 100; ++i) {
    21. Task task(pResource->resourcesThreadFun);
    22. manager.addTask(std::move(task));
    23. }
    24. std::cout << "main thread over!" << std::endl;
    25. return 0;
    26. }

  • 相关阅读:
    ChatTTS web应用;基于文本指导的图像生成;使用Groq和Llama3在几秒内生成整本书;协作机器人画家,可以根据语言描述或图像在画布上作画
    PyTorch学习笔记-TensorBoard
    Java异常
    【DS】树和二叉树的理论知识梳理
    设计模式行为型-模板模式
    给小白的 PG 容器化部署教程(下)
    wallys/QCN9074/WiFi 6 (802.11ax) 4×4 MU-MIMO 2.4GHz Single Band Wireless Module
    五个维度着手MySQL的优化
    浏览器无痕浏览还能查到记录吗,如何开启无痕模式
    Java刷题day29
  • 原文地址:https://blog.csdn.net/qianlixiaomage/article/details/138169573