C++ post(boost::bind(&;myFunction,this,attr1,attri2)不会发布工作
我有一个程序,它从用户那里获取多个输入,并根据输入将工作发布到线程池,如下所示:C++ post(boost::bind(&;myFunction,this,attr1,attri2)不会发布工作,c++,multithreading,boost,boost-asio,threadpool,C++,Multithreading,Boost,Boost Asio,Threadpool,我有一个程序,它从用户那里获取多个输入,并根据输入将工作发布到线程池,如下所示: while (getline (file, input)){ if(input =='a'){ ioService_.post(boost::bind(&myClass::myFunction, this, input, counter)); } //end if else if(input=='b'){ i
while (getline (file, input)){
if(input =='a'){
ioService_.post(boost::bind(&myClass::myFunction, this, input, counter));
} //end if
else if(input=='b'){
ioService_.post(boost::bind(importedFunction, input));
}
else{
break;
}
}//end while
threadpool.join_all();
}
函数importedFunction是从另一个类导入的,代码可以正确地使用它,但对于myFunction函数,它不会进行后期工作
我最初是这样绑定函数的:
ioService_.post(boost::bind(myClass::myFunction, input, counter));
它给了我一个错误,那就是:
/usr/local/include/boost/bind/bind.hpp:75:22:Type“void”
(多线程::*)(std::_1::basic_string,int)“”不能使用
在“::”之前,因为它没有成员
然后我换成这个解决了错误:
ioService_.post(boost::bind(&myClass::myFunction, this, input, counter));
但是现在工作没有提交,我不知道为什么。我怀疑您可能忘记了
运行io\U服务
如果是,请添加
ioService_.run();
某处
也许还可以阅读有关io_service::work
的内容,以使服务在任务之间保持活动状态。服务线程可能完成了运行()
在发布工作之前。我也遇到了同样的问题。这是因为io_服务::工作。此复制构造函数的任务通知io_服务工作正在开始
如果其他人需要,这里有一个工作示例
#include <iostream>
#include <unistd.h>
#include <iostream>
#include <thread>
#include <vector>
#include <algorithm>
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>
#include <boost/thread.hpp>
#include <boost/bind.hpp>
#include <boost/asio.hpp>
#include <boost/move/move.hpp>
#include <boost/make_unique.hpp>
namespace asio = boost::asio;
typedef boost::packaged_task<int> task_t;
typedef boost::shared_ptr<task_t> ptask_t;
class Task
{
public:
int execute(std::string command)
{
//TODO actual logic
std::cout<< "\nThread:" << command << std::endl;
int sum = 0;
for(int i = 0; i < 5; i++)
{
sum+=i;
}
return sum;
}
};
void push_job(Task* worker, std::string seconds, boost::asio::io_service& io_service
, std::vector<boost::shared_future<int> >& pending_data) {
ptask_t task = boost::make_shared<task_t>(boost::bind(&Task::execute, worker, seconds));
boost::shared_future<int> fut(task->get_future());
pending_data.push_back(fut);
io_service.post(boost::bind(&task_t::operator(), task));
}
int main()
{
Task* taskPtr = new Task();
boost::asio::io_service io_service;
boost::thread_group threads;
std::unique_ptr<boost::asio::io_service::work> service_work;
service_work = boost::make_unique<boost::asio::io_service::work>(io_service);
for (int i = 0; i < boost::thread::hardware_concurrency() ; ++i)
{
threads.create_thread(boost::bind(&boost::asio::io_service::run,
&io_service));
}
std::vector<boost::shared_future<int> > pending_data; // vector of futures
push_job(taskPtr, "4", io_service, pending_data);
push_job(taskPtr, "5", io_service, pending_data);
push_job(taskPtr, "6", io_service, pending_data);
push_job(taskPtr, "7", io_service, pending_data);
boost::wait_for_all(pending_data.begin(), pending_data.end());
int total_sum = 0;
for(auto result : pending_data){
total_sum += result.get();
}
std::cout<< "Total sum: "<< total_sum << std::endl;
return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
名称空间asio=boost::asio;
typedef boost::打包的任务;
typedef boost::共享任务;
课堂任务
{
公众:
int执行(std::string命令)
{
//TODO实际逻辑
std::coutI做到了这一点,我有一个初始化池的例程,即创建线程并创建工作。如果我只使用导入的函数,那么每当我使用我在类中创建的函数时,它都会工作。很难理解“导入的函数”和“myfunction”的含义因为我们看不到这方面的任何内容。请共享一个显示问题的SSCCE。对于导入的函数,它在另一个标题中定义#包含类2,我在文件中包含类,然后我使用它。我的函数是我在我使用的类中定义的函数