在我的程序中,我有一个Master类(grpc异步客户端),它将工作分配给所有Workers(grpc异步服务器),它通过一个名为WorkerClient的类来处理所有通信开销。
我的Master类尝试设置(分离的)后台线程以等待来自服务器的响应,这就是我遇到困难的地方。
我尝试过的一个策略是让我的“AsyncComplete”函数成为WorkerClient类的一部分(与基于grpc.io上的Async-Greeter教程的涉及异步客户机 - 服务器grpc的最新程序的方法相同),这种方法的课程详情如下。
class WorkerClient {
public:
WorkerClient(std::shared_ptr<grpc::Channel> channel);
void MapReduceWork(masterworker::WorkReq);
void AsyncComplete(void *, void *, int);
struct AsyncClientCall {
masterworker::WorkReply reply;
grpc::ClientContext context;
grpc::Status status;
std::unique_ptr<grpc::ClientAsyncResponseReader<masterworker::WorkReply>> rsp_reader;
};
std::mutex mtx;
std::unique_ptr<masterworker::MasterWorker::Stub> stub;
grpc::CompletionQueue cq;
masterworker::WorkReq work_req;
ClientState state;
};
class Master {
public:
Master(const MapReduceSpec&, const std::vector<FileShard>&);
......
private:
std::mutex mtx;
std::vector<FileShard> map_tasks;
std::vector<ReduceTask *> reduce_tasks;
std::vector<WorkerClient *> clients;
std::vector<std::string> m_interm_files;
std::vector<FileShard> shards;
MapReduceSpec mr_config;
};
以下是相关的功能细节
void WorkerClient::AsyncComplete(void *c, void *m, int client_num)
{
void *got_tag = NULL;
bool ok = false;
Master *mast = static_cast<Master*>(m);
WorkerClient *client = static_cast<WorkerClient*>(c);
cq.Next(&got_tag, &ok);
if (ok == false) {
fprintf(stderr, "cq->Next false!\n");
return;
}
......
}
创建线程的调用
void Master::RunMapJob()
{
// clients[] is of type WorkerClient
ptr = new std::thread(&WorkerClient::AsyncComplete,
static_cast<void*>(clients[i]), static_cast<void*>(this), i);
ptr->detach();
......
}
最后,问题本身
g++ -c master.cc -I../external/include -std=c++11 -g
In file included from /usr/include/c++/4.8/mutex:42:0,
from master.h:3,
from master.cc:2:
/usr/include/c++/4.8/functional: In instantiation of ‘struct std::_Bind_simple<std::_Mem_fn<void (WorkerClient::*)(void*, void*, int)>(void*, void*, int)>’:
/usr/include/c++/4.8/thread:137:47: required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (WorkerClient::*)(void*, void*, int); _Args = {void*, void*, int&}]’
master.cc:223:65: required from here
/usr/include/c++/4.8/functional:1697:61: error: no type named ‘type’ in ‘class std::result_of<std::_Mem_fn<void (WorkerClient::*)(void*, void*, int)>(void*, void*, int)>’
typedef typename result_of<_Callable(_Args...)>::type result_type;
^
/usr/include/c++/4.8/functional:1727:9: error: no type named ‘type’ in ‘class std::result_of<std::_Mem_fn<void (WorkerClient::*)(void*, void*, int)>(void*, void*, int)>’
_M_invoke(_Index_tuple<_Indices...>)
^
make: *** [master.o] Error 1
到目前为止,我已经排除了显而易见的事情,比如不使用std :: ref将变量引用传递给线程函数,并且通常不传递正确的线程函数参数。我还尝试使AsyncComplete成为一个静态独立函数,但由于调用cq.Next(更改为WorkerClient-&gt; cq.Next())抛出了std :: bad_alloc异常,因此无效(因为CompletionQueue代码在幕后,所以无法想出那个。)
如果有帮助,我能够找到/usr/include/c++/4.8/functional在编译期间似乎窒息的行(下面)
typedef typename result_of<_Callable(_Args...)>::type result_type;
任何人都可以帮我解释为什么这不能编译,以及适当的修复可能是什么样的?我很想在这里发帖提问,所以反馈很受欢迎,我知道这很长,但我想确保收到所有信息。
提前致谢。
[系统详细信息:ubuntu 14.04,protobufs 3.0,从protobufs 3.0源代码构建的grpc]
答案 0 :(得分:0)
这一行是问题所在:
ptr = new std::thread(&WorkerClient::AsyncComplete,
static_cast<void*>(clients[i]), static_cast<void*>(this), i)
std::thread
的构造函数接受一个仿函数和参数。您提供&WorkerClient::AsyncComplete
而未向其提供WorkerClient
的实例。我猜客户端实际上包含在clients[i]
中。如果你试试
ptr = new std::thread(std::bind(&WorkerClient::AsyncComplete, clients[i], static_cast<void*>(clients[i]), static_cast<void*>(this), i))
它应该编译。