我正在查看这个async_server示例。我对以下代码段感兴趣,
while (true) {
// Block waiting to read the next event from the completion queue. The
// event is uniquely identified by its tag, which in this case is the
// memory address of a CallData instance.
// The return value of Next should always be checked. This return value
// tells us whether there is any kind of event or cq_ is shutting down.
GPR_ASSERT(cq_->Next(&tag, &ok));
GPR_ASSERT(ok);
/*
How to enqueue this request on an existing thread pool
For example,
pool.enqueue() {
/* what to enqueue so that when one worker thread of my thread pool
pop one requests it can decide what handler to execute */
}
*/
// do not process request in this thread, commenting it
// static_cast<CallData*>(tag)->Proceed();
}
在我的设计中,以上循环由一个专用线程执行,并且该线程基本上将请求推送到thread-safe
队列中。来自thread_safe
队列的池弹出请求对象中的工作线程,并在多核环境中并行处理请求。
我有一些简单的处理函数,应该由工作线程从队列中弹出的特定请求触发这些函数。将被推入队列的对象应该具有足够的信息,以便工作线程可以决定要执行的处理程序。
例如;处理程序功能可以是非常简单的CPU绑定任务。
void handler1( .... some argument ) {
// a CPU bound for loop
// reply someting to the client }
我的问题是:假设thread_safe队列提供以下API,如何将标记信息放入用户定义的thread_safe
队列中:
enqueue(someObj task) {
// task should have tag information required for the worker threads to select
// proper handler function.
// I need to know the following, what should I push to the queue or what
// other code changes I need to do to push something meaningful for the
// worker threads. Should I create multiple services? Or can I handle
// multiple handler functions by having the same service.
}
当我说handler function
时,是指async_server链接中的现有代码。
// inside proceed() function
// The actual processing.
std::string prefix("Hello ");
reply_.set_message(prefix + request_.name());
谢谢。