Async client completion queue - thread safety

577 views
Skip to first unread message

akshitha...@gmail.com

unread,
Oct 28, 2017, 8:19:08 PM10/28/17
to grpc.io
I want to have multiple async client threads (each thread may or may not communicate with a different server) that send requests asynchronously using a single global completion queue:

global cq:
CompletionQueue* cq = new CompletionQueue();

actual rpc call:
AsyncClientCall* call = new AsyncClientCall;
call->response_reader = stub_->AsyncRPC(&call->context, request, cq);

I also have multiple response collection threads, that wish to collect responses from this global cq using Next():
void* tag;
bool ok = false;
cq.Next(&tag, &ok);

I know that Next() is a thread safe operation. I want to find out if thread safety still exists between the client threads that are trying to send a request using the global cq, and the response collection threads that are listening on the same global cq.
If this design is not thread safe, what is the best way to implement this functionality without having to poll multiple completion queues?

Thanks!

Regards,
Akshitha

Vijay Pai

unread,
Oct 30, 2017, 1:58:55 PM10/30/17
to grpc.io
The design that you've given is thread-safe as stated. CQs and all flavors of the Next operation have been designed to be thread-safe from the beginning.

However: moving forward, let me suggest that you use the 2-phase async client call APIs. 1-phase is sufficient for unary calls, but any kind of streaming call will need additional synchronization unless you use the 2-phase calls. An example of the 2-phase unary call is given in examples/cpp/helloworld but the basic form would be:

call->response_reader = stub->PrepareAsyncRPC(&call->context, request, cq);
call->response_reader->StartCall();

For consistency, we're recommending the use of the 2-phase calls for multi-threaded code even for unary calls. We will eventually provide guidance along these lines when building the library with debug options.

Best regards,
Vijay Pai
Reply all
Reply to author
Forward
0 new messages