grpc c++ example有以下评论。
// Spawn a new CallData instance to serve new clients while we process
// the one for this CallData.
我对这句话有些困惑,因为这似乎表明新的CallData实例可以潜在地同时为其他客户端提供服务。 (这是事件循环吗?)
但是,在该示例中我看不到正在创建任何新线程。我是否正确地假设没有创建新线程,并且CallData
可能作用于任何共享变量,并且不需要互斥体?还是需要互斥锁?
例如,如果示例中的代码更改为以下代码,我是否需要互斥锁?
...
else if (status_ == PROCESS) {
// Spawn a new CallData instance to serve new clients while we process
// the one for this CallData. The instance will deallocate itself as
// part of its FINISH state.
new CallData(service_,cq_);
// Do I need a mutex here?
// mutex.lock()
service->do_something_to_variable();
// mutex.unlock()
std::string prefix("Hello ");
reply_.set_message(prefix + request_.name());