| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 
 | #include <iostream>#include <queue>
 #include <vector>
 #include <array>
 #include <thread>
 #include <future>
 #include <mutex>
 #include <condition_variable>
 #include <functional>
 #include <memory>
 
 class ThreadPool
 {
 private:
 std::vector<std::thread> m_threads;
 std::queue<std::function<void()>> m_tasks;
 std::mutex m_mutex;
 std::condition_variable m_condition;
 uint16_t m_threads_num;
 bool m_stop;
 
 public:
 ThreadPool(int _threadNum) : m_threads_num(_threadNum), m_stop(false)
 {
 std::cout << "Thread pool constructing..." << std::endl;
 for (size_t i = 0; i < m_threads_num; i++)
 {
 m_threads.emplace_back([this]()
 {
 while (true)
 {
 std::function<void()> task;
 {
 std::unique_lock<std::mutex> lock(m_mutex);
 m_condition.wait(lock, [this]() { return !m_tasks.empty() || m_stop; });
 if (m_stop && m_tasks.empty())
 {
 return;
 }
 task = std::move(m_tasks.front());
 m_tasks.pop();
 }
 task();
 } });
 }
 }
 
 template <typename F, typename... Args>
 std::future<std::result_of_t<F(Args...)>> addTask(F &&_task, Args &&..._args)
 {
 using return_type = std::result_of_t<F(Args...)>;
 auto task = std::make_shared<std::packaged_task<return_type()>>(
 std::bind(std::forward<F>(_task), std::forward<Args>(_args)...));
 
 {
 std::lock_guard<std::mutex> lock(m_mutex);
 m_tasks.emplace([task]()
 { (*task)(); });
 }
 std::future<return_type> future = task->get_future();
 return future;
 }
 
 void stop()
 {
 {
 std::unique_lock<std::mutex> lock(m_mutex);
 m_stop = true;
 }
 m_condition.notify_all();
 }
 
 ~ThreadPool()
 {
 stop();
 
 for (auto &thread : m_threads)
 {
 if (thread.joinable())
 thread.join();
 }
 std::cout << "Thread pool destructed." << std::endl;
 }
 };
 
 int calculate(const int &n)
 {
 std::cout << "Task: " << n << " calculating: " << n << " * " << n << std::endl;
 return n * n;
 }
 
 int main()
 {
 ThreadPool pool(4);
 
 std::array<std::future<int>, 10> futures;
 for (int i = 0; i < 10; i++)
 {
 futures[i] = pool.addTask(calculate, i);
 }
 
 for (int i = 0; i < 10; i++)
 {
 std::cout << "Calculate result: " << futures[i].get() << std::endl;
 }
 
 return 0;
 }
 
 |