Is this a valid way to multi thread RPC handlers in gRPC async server in C++?

2,394 views
Skip to first unread message

Rajanarayana A

unread,
Nov 15, 2021, 12:59:34 AM11/15/21
to grpc.io
Hello, 

Extremely sorry if this is a basic question. 

I'm trying to work on the basic async gRPC C++ code as mentioned in https://github.com/grpc/grpc/tree/master/examples/cpp/helloworld

Aim is to experiment multi threading of RPC handlers. Can someone please let me know that whether the programming logic is correct? ( Please refer to the stackoverflow link -   https://stackoverflow.com/questions/69944030/is-this-a-valid-way-to-multi-thread-rpc-handlers-in-grpc-async-server-in-c )

Few questions,

  • Is the completion queue thread-safe?
  • I see that there is a possibility of one instance of CallData being accessed in multiple threads (returned as part of tag). Is CallData thread-safe here or do we need to have a mutex for the same?

Please note that this is an incomplete program.

Appreciate any help on this.

Thanks and Regards,
Raj

yas...@google.com

unread,
Dec 1, 2021, 1:59:59 PM12/1/21
to grpc.io
Completion queues are thread-safe.
CallData is an application-level construct. gRPC itself won't be accessing the internals of the struct. It just uses it as an opaque pointer. If the application level logic is such that CallData can be accessed from multiple threads, you would need to synchronize access to it.

Hope that helps!

Christopher Warrington - MSFT

unread,
Dec 2, 2021, 12:03:28 PM12/2/21
to grpc.io
On Sunday, November 14, 2021 at 9:59:34 PM UTC-8 Rajanarayana A wrote:

> I see that there is a possibility of one instance of CallData being
> accessed in multiple threads (returned as part of tag). Is CallData
> thread-safe here or do we need to have a mutex for the same?

There's an older thread on a similar topic, "How to implement async gRPC
with more than one method?" [1]. It talks about how to manage multiple
CallData instances at once, which may be relevant to what you're doing.

[1]: https://groups.google.com/g/grpc-io/c/bXMmfah57h4/m/EUg2B8o1AgAJ

--
Christopher Warrington
Microsoft Corp
Reply all
Reply to author
Forward
0 new messages