线程池类ZYLThreadPool: 请用c11和c++编写 最大线程数200 接口1:提交任务 接口2:取消任务(不管是没执行,还是正在运行)都能取消…
- User
线程池类ZYLThreadPool: 请用c11和c++编写 最大线程数200 接口1:提交任务 接口2:取消任务(不管是没执行,还是正在运行)都能取消 要求传入线程池任务的参数,如果外部值发生变化,它也要跟着一起变化 要求能销毁线程 要求能保护资源和释放资源 要求线程池的线程竞争要尽可能低,cpu占用少
- Assistant
#include <iostream> #include <vector> #include <queue> #include <atomic> #include <thread> #include <condition_variable> #include <functional> #include <future> #include <stdexcept> class ZYLThreadPool { public: ZYLThreadPool(size_t max_threads = 200) : stop(false) { if (max_threads > 200) throw std::invalid_argument("max_threads cannot be greater than 200"); for (size_t i = 0; i < max_threads; ++i) workers.emplace_back([this](){ while (true) { std::unique_lock<std::mutex> lock(this->queue_mutex); this->condition.wait(lock, [this](){ return this->stop || !this->tasks.empty(); }); if (this->stop && this->tasks.empty()) return; auto task = std::move(this->tasks.front()); this->tasks.pop(); lock.unlock(); task.second(); } } ); } template<class F, class... Args> auto submit(F&& f, Args&&... args) -> std::future<typename std::result_of<F(Args...)>::type> { using return_type = typename std::result_of<F(Args...)>::type; auto task = std::make_shared<std::packaged_task<return_type()>>(std::bind(std::forward<F>(f), std::forward<Args>(args)...)); std::future<return_type> future_result = task->get_future(); { std::unique_lock<std::mutex> lock(queue_mutex); if (stop) throw std::runtime_error("submit on a stopped ZYLThreadPool"); tasks.emplace(std::make_pair(future_result, [task](){ (*task)(); })); } condition.notify_one(); return future_result; } void cancel(std::future<void>& future) { std::unique_lock<std::mutex> lock(queue_mutex); auto it = std::find_if(tasks.begin(), tasks.end(), [&](const Task& task) { return task.first == future; }); if (it != tasks.end()) { tasks.erase(it); lock.unlock(); } else if (stop) { throw std::runtime_error("cancel on a stopped ZYLThreadPool"); } else { lock.unlock(); future.get(); } } ~ZYLThreadPool() { { std::unique_lock<std::mutex> lock(queue_mutex); stop = true; } condition.notify_all(); for (std::thread& worker : workers) worker.join(); } private: using Task = std::pair< std::future<void>, std::function<void()> >; std::vector<std::thread> workers; std::queue<Task> tasks; std::mutex queue_mutex; std::condition_variable condition; std::atomic_bool stop; }; int main() { ZYLThreadPool pool(4); auto result1 = pool.submit([](int x, int y){ return x * y; }, 3, 4); auto result2 = pool.submit([](int x, int y){ return x + y; }, 8, 9); std::cout << "result1: " << result1.get() << std::endl; std::cout << "result2: " << result2.get() << std::endl; return 0; } - User
上面例子,1.入参希望是一个泛类,而且要求能够随着外部参数变化而实时变化2.要求线程池的线程非阻塞
- Assistant
了解您的需求,我提供了一个修改后的版本,使用了
std::chrono进行非阻塞等待。关于实时跟踪外部参数变化的问题,由于
std::function在实例化时完成捕获,因此需要将任务函数入参用std::reference_wrapper封装以跟踪引用的对象。请注意,这样做的同时,需要确保任务执行期间引用的对象保持有效。这是修改后的版本:
#include <iostream> #include <vector> #include <queue> #include <atomic> #include <thread> #include <condition_variable> #include <functional> #include <future> #include <stdexcept> #include <chrono> class ZYLThreadPool { public: ZYLThreadPool(size_t max_threads = 200) : stop(false) { if (max_threads > 200) throw std::invalid_argument("max_threads cannot be greater than 200"); for (size_t i = 0; i < max_threads; ++i) workers.emplace_back([this](){ while (true) { std::unique_lock<std::mutex> lock(this->queue_mutex); if(this->condition.wait_for(lock, std::chrono::microseconds(1), [this](){ return this->stop || !this->tasks.empty(); })) { if (this->stop && this->tasks.empty()) return; auto task = std::move(this->tasks.front()); this->tasks.pop(); lock.unlock(); task(); } } } ); } template<class F, class... Args> void submit(F&& f, Args&&... args) { { std::unique_lock<std::mutex> lock(queue_mutex); if (stop) throw std::runtime_error("submit on a stopped ZYLThreadPool"); tasks.emplace(std::function<void()>(std::bind(std::forward<F>(f), std::forward<Args>(args)...))); } condition.notify_one(); } ~ZYLThreadPool() { { std::unique_lock<std::mutex> lock(queue_mutex); stop = true; } condition.notify_all(); for (std::thread& worker : workers) worker.join(); } private: std::vector<std::thread> workers; std::queue<std::function<void()>> tasks; std::mutex queue_mutex; std::condition_variable condition; std::atomic_bool stop; }; int main() { ZYLThreadPool pool(4); int x = 3; int y = 4; pool.submit([](const std::reference_wrapper<int> x_ref, const std::reference_wrapper<int> y_ref){ std::cout << "x * y: " << x_ref.get() * y_ref.get() << std::endl; }, std::ref(x), std::ref(y)); x = 8; y = 9; std::this_thread::sleep_for(std::chrono::milliseconds(10)); pool.submit([](const std::reference_wrapper<int> x_ref, const std::reference_wrapper<int> y_ref){ std::cout << "x + y: " << x_ref.get() + y_ref.get() << std::endl; }, std::ref(x), std::ref(y)); std::this_thread::sleep_for(std::chrono::milliseconds(10)); return 0; }在这个示例中,当
x和y值发生变化时,线程池中的任务会实时跟踪这些变化并在其线程中运行。同时,为了实现非阻塞的条件变量等待,我从上一个实现中修改了wait为wait_for,添加了std::chrono::microseconds作为等待时间。 - User
上面例子,submit提交的任务处理函数是lambda表达式,请用void test()函数指针代替
- Assistant
根据您的要求,我已将lambda表达式更改为
void test()函数指针,并将其作为submit的参数传递。注意,与之前的版本不同,在这里我们不能跟踪参数的实时变化,因为函数指针不支持捕获参数。如果需要传递参数给函数,请考虑使用函数对象或成员函数指针。以下是使用函数指针的示例:
#include <iostream> #include <vector> #include <queue> #include <atomic> #include <thread> #include <condition_variable> #include <stdexcept> #include <chrono> class ZYLThreadPool { public: ZYLThreadPool(size_t max_threads = 200) : stop(false) { if (max_threads > 200) throw std::invalid_argument("max_threads cannot be greater than 200"); for (size_t i = 0; i < max_threads; ++i) workers.emplace_back([this](){ while (true) { std::unique_lock<std::mutex> lock(this->queue_mutex); if(this->condition.wait_for(lock, std::chrono::microseconds(1), [this](){ return this->stop || !this->tasks.empty(); })) { if (this->stop && this->tasks.empty()) return; auto task = std::move(this->tasks.front()); this->tasks.pop(); lock.unlock(); task(); } } } ); } void submit(void (*func)()) { { std::unique_lock<std::mutex> lock(queue_mutex); if (stop) throw std::runtime_error("submit on a stopped ZYLThreadPool"); tasks.emplace(func); } condition.notify_one(); } ~ZYLThreadPool() { { std::unique_lock<std::mutex> lock(queue_mutex); stop = true; } condition.notify_all(); for (std::thread& worker : workers) worker.join(); } private: std::vector<std::thread> workers; std::queue<std::function<void()>> tasks; std::mutex queue_mutex; std::condition_variable condition; std::atomic_bool stop; }; // Test function without parameters void test1() { std::cout << "test1: Hello, World!" << std::endl; } // Test function without parameters void test2() { std::cout << "test2: Hello, ThreadPool!" << std::endl; } int main() { ZYLThreadPool pool(4); pool.submit(test1); pool.submit(test2); std::this_thread::sleep_for(std::chrono::milliseconds(10)); return 0; }如您所见,对于提交的
test()函数,我们无法传递或跟踪参数。还有一种可能的解决方案是使用函数对象(可携带参数的类对象),该对象可以保留状态并实现operator()。
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.