|
| 1 | +#include <condition_variable> |
| 2 | +#include <functional> |
| 3 | +#include <iostream> |
| 4 | +#include <mutex> |
| 5 | +#include <queue> |
| 6 | +#include <thread> |
| 7 | + |
| 8 | +int plus_one(int n) { |
| 9 | + std::cout << "In plus_one" << std::endl; |
| 10 | + return n + 1; |
| 11 | +} |
| 12 | + |
| 13 | +class ThreadPool { |
| 14 | + public: |
| 15 | + ThreadPool(size_t num_threads = std::thread::hardware_concurrency()) { |
| 16 | + for (uint32_t i = 0; i < num_threads; ++i) { |
| 17 | + threads.emplace_back(std::thread(&ThreadPool::ThreadLoop, this)); |
| 18 | + } |
| 19 | + } |
| 20 | + ~ThreadPool() { |
| 21 | + { |
| 22 | + std::unique_lock<std::mutex> lock(queue_mutex); |
| 23 | + terminated = true; |
| 24 | + } |
| 25 | + condition.notify_all(); |
| 26 | + for (std::thread& thread : threads) { |
| 27 | + thread.join(); |
| 28 | + } |
| 29 | + } |
| 30 | + void enqueue(const std::function<void()>& job) { |
| 31 | + { |
| 32 | + std::unique_lock<std::mutex> lock(queue_mutex); |
| 33 | + if (terminated) |
| 34 | + throw std::runtime_error("enqueue on stopped ThreadPool"); |
| 35 | + jobs.push(job); |
| 36 | + } |
| 37 | + condition.notify_one(); |
| 38 | + } |
| 39 | + |
| 40 | + private: |
| 41 | + void ThreadLoop() { |
| 42 | + while (true) { |
| 43 | + std::function<void()> job; |
| 44 | + { |
| 45 | + std::unique_lock<std::mutex> lock(queue_mutex); |
| 46 | + condition.wait(lock, [this] { return !jobs.empty() || terminated; }); |
| 47 | + if (terminated && jobs.empty()) |
| 48 | + return; |
| 49 | + job = jobs.front(); |
| 50 | + jobs.pop(); |
| 51 | + } |
| 52 | + job(); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + bool terminated = false; |
| 57 | + std::mutex queue_mutex; |
| 58 | + std::condition_variable condition; |
| 59 | + std::vector<std::thread> threads; |
| 60 | + std::queue<std::function<void()>> jobs; |
| 61 | +}; |
| 62 | + |
| 63 | +void thread_func(int job_index) { |
| 64 | + std::cout << __FUNCTION__ << " (job index = " << job_index |
| 65 | + << ") running on thread " << std::this_thread::get_id() << "\n"; |
| 66 | + std::cout << "Calling function plus_one(int)\nResult = " |
| 67 | + << plus_one(job_index) << "\n"; |
| 68 | +} |
| 69 | + |
| 70 | +void thread_sleep(int job_index, int sleep_sec) { |
| 71 | + std::cout << __FUNCTION__ << " (job index = " << job_index |
| 72 | + << ") starting on thread " << std::this_thread::get_id() << "\n"; |
| 73 | + std::this_thread::sleep_for(std::chrono::seconds(sleep_sec)); |
| 74 | + std::cout << __FUNCTION__ << " (job index = " << job_index |
| 75 | + << ") finished on thread " << std::this_thread::get_id() << "\n"; |
| 76 | +} |
| 77 | + |
| 78 | +int main() { |
| 79 | + ThreadPool tp1(2000); |
| 80 | + ThreadPool tp2; |
| 81 | + std::cout << "main() running on thread " << std::this_thread::get_id() |
| 82 | + << "\n"; |
| 83 | + std::cout |
| 84 | + << "Program is expecting stdin. Please attach debugger and hit enter to continue\n"; |
| 85 | + std::cin.get(); |
| 86 | + // At least one of the thread in tp1 will be sceduled with a task twice or |
| 87 | + // more if num_jobs is larger than #threads in tp1. |
| 88 | + int num_jobs = 3000; |
| 89 | + for (int i = 0; i < num_jobs; ++i) { |
| 90 | + tp1.enqueue(std::bind(thread_func, i)); |
| 91 | + } |
| 92 | + // We may or may not hit the breakpoint thread_sleep here, as the tread pool |
| 93 | + // might finish executing before debugger release the bp for tp1. |
| 94 | + // To make sure we hit the bp, we can increase the sleep time in the call. |
| 95 | + for (int i = 0; i < num_jobs; ++i) { |
| 96 | + tp2.enqueue([i] { thread_sleep(i, 1); }); |
| 97 | + } |
| 98 | + for (int i = 0; i < num_jobs; ++i) { |
| 99 | + tp1.enqueue(std::bind(thread_sleep, i, 1)); |
| 100 | + return 0; |
| 101 | + } |
| 102 | +} |
0 commit comments