You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

thread_pool.h 2.5 kB

5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /**
  2. * Copyright 2020 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef MINDSPORE_CCSRC_COMMON_THREAD_POOL_H_
  17. #define MINDSPORE_CCSRC_COMMON_THREAD_POOL_H_
  18. #include <mutex>
  19. #include <condition_variable>
  20. #include <thread>
  21. #include <vector>
  22. #include <queue>
  23. #include <string>
  24. #include <atomic>
  25. #include <memory>
  26. #include <utility>
  27. #include <functional>
  28. #include <iostream>
  29. #include "utils/log_adapter.h"
  30. namespace mindspore {
  31. const int kCoreThreadNum = 3;
  32. const int kDefaultMaxThreadNum = 8;
  33. enum Status { FAIL = -1, SUCCESS = 0 };
  34. using Task = std::function<int()>;
  35. class Queue {
  36. public:
  37. Queue() = default;
  38. ~Queue() = default;
  39. bool Enqueue(Task *task);
  40. bool Dequeue(Task **out);
  41. std::atomic_int task_size_ = {0};
  42. private:
  43. std::atomic_int head_ = {0};
  44. std::atomic_int tail_ = {0};
  45. Task *buffer_[2]{};
  46. };
  47. class ThreadPool {
  48. public:
  49. ~ThreadPool();
  50. ThreadPool(const ThreadPool &) = delete;
  51. ThreadPool &operator=(const ThreadPool &) = delete;
  52. static ThreadPool *GetInstance();
  53. // Use the tasks' size of threads to execute these tasks, one thread execute one task.
  54. bool LaunchMultipleTask(const std::vector<Task> &tasks);
  55. private:
  56. ThreadPool();
  57. bool SetThreadPool(int config_thread_num);
  58. void AddNewThread(int add_num);
  59. void AddRunThread(int num);
  60. void SubRunThread(int num);
  61. bool CheckResult();
  62. int cur_thread_nums_{0};
  63. int cur_thread_run_nums_{0};
  64. int core_thread_num_{kCoreThreadNum};
  65. int max_thread_num_{kDefaultMaxThreadNum};
  66. std::mutex pool_mtx_;
  67. std::mutex thread_mtx_;
  68. std::condition_variable queue_ready_;
  69. std::atomic_bool exit_run_ = {false};
  70. std::vector<std::atomic_bool *> activate_list_{};
  71. std::vector<std::thread> thread_list_{};
  72. std::vector<std::shared_ptr<Queue>> queue_list_{};
  73. std::vector<std::pair<int, std::pair<bool, int>>> error_info_{};
  74. };
  75. } // namespace mindspore
  76. #endif // MINDSPORE_CCSRC_COMMON_THREAD_POOL_H_