#include <iostream> #include <thread> #include <deque> #include <functional> #include…
- User
#include #include #include #include #include #include #include <condition_variable> #include #include
class ZYLThreadPool { public: ZYLThreadPool(size_t maxThreads = 200): _maxThreads(maxThreads), _stop(false) { worker_threads.reserve(_maxThreads); for(size_t i = 0; i < _maxThreads; ++i) { worker_threads.emplace_back([this] { for(;;) { std::function<void()> task; { std::unique_lockstd::mutex lock(this->_mutex); this->_condVar.wait(lock, [this] { return this->_stop || !this->_taskQueue.empty(); }); if(this->_stop && this->_taskQueue.empty()) return; task = std::move(this->_taskQueue.front()); this->_taskQueue.pop_front(); } task(); } }); } }
codetemplate<typename F, typename… Args> std::future<typename std::result_of<F(Args…)>::type> submit(F&& f, Args&&… args) { typedef typename std::result_of<F(Args…)>::type return_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> result = task->get_future(); { std::unique_lock<std::mutex> lock(_mutex); if (_stop) { throw std::runtime_error(“Submit on stopped ZYLThreadPool”); } _taskQueue.emplace_back([task] { (*task)(); }); } _condVar.notify_one(); return result; } void cancel() { std::unique_lock<std::mutex> lock(_mutex); if (!_stop) { _stop = true; _condVar.notify_all(); } for(std::thread &worker: worker_threads) worker.join(); } size_t getNumTasks() { std::unique_lock<std::mutex> lock(_mutex); return _taskQueue.size(); } ~ZYLThreadPool() { cancel(); }private: std::vectorstd::thread worker_threads; std::deque<std::function<void()>> _taskQueue; size_t _maxThreads; std::mutex _mutex; std::condition_variable _condVar; bool _stop; };
class Test { public: Test(double devid): _devid(devid), _running(true) { }
- Assistant
- User
仍然报错:error: extended character … is not valid in an identifier 34 | template<typename F, typename… Args>,error: extended character … is not valid in an identifier 38 | auto task = std::make_shared<std::packaged_task<return_type()>>(std::bind(std::forward(f), std::forward(args)…));rror: ‘typename…’ has not been declared 34 | template<typename F, typename… Args>;error: ‘Args…’ was not declared in this scope 35 | std::future<typename std::result_of<F(Args…)>::type> submit(F&& f, Args&&… args) ;rror: ‘’ is not a template [-fpermissive] 35 | std::future<typename std::result_of<F(Args…)>::type> submit(F&& f, Args&&… args) {error: ‘Args…’ was not declared in this scope 36 | typedef typename std::result_of<F(Args…)>::type return_type;
- Assistant
- User
请帮忙写一个Makefile进行编译测试
- Assistant
这是一个简单的Makefile,用于编译上面提供的代码:
codeCXX = g++ CXXFLAGS = -std=c++11 -Wall -Wextra -O2 TARGET = ThreadPoolExample SOURCES = ThreadPoolExample.cpp all: ${TARGET} ${TARGET}: ${SOURCES} ${CXX} ${CXXFLAGS} ${SOURCES} -o ${TARGET} clean: rm -rf ${TARGET}将代码保存为 ThreadPoolExample.cpp,然后将这个Makefile存储到与 ThreadPoolExample.cpp 同一目录中。确保Makefile和源文件位于同一目录中,并在命令行中运行:
codemake这将使用 g++ 编译器和 C++11 标准编译您的代码。编译完成后,可以通过运行
./ThreadPoolExample来执行代码。如果您要清除编译生成的可执行文件,可以运行:
codemake clean