Callback based Async programming model in C++

71 views
Skip to first unread message

sumuk...@gmail.com

unread,
Mar 28, 2019, 9:16:17 PM3/28/19
to grpc.io

I created an issue in the github repo (https://github.com/grpc/grpc/issues/18550) and realized this might be a better place to have a conversation about this topic:


The C# API's for the proto files consist of Task based client and server API's in the generated code - which allows for "await client->MethodAsync" to be invoked without blocking the thread.


For C++ though, I do not see this paradigm. The only available option seems like a polling based mechanism on a completion queue. I did notice a method named "AsyncNext" added by @vjpai , but again we need to provide a deadline and if there is a timeout, there is no good value to provide as the parameter the next time we need invoke the method as the caller has no idea how long it would take for the work to be done.


I am new to async programming patterns on linux environments and am wondering if I am missing something obvious. On windows, we have always passed callbacks in async API's. These callbacks are invoked by the callee with a context when the API is completed. This way, we never need to poll to check if the work is completed or not. Cancellations and timeouts are handled similarly as well.


In the C# API mentioned above, the await key word in .NET creates an implicit callback and yields the thread until the callback is ready to run. The new C++ standards (C++20) have also approved the coroutines into the standards and are expected to release it in the coming year.


Is there a reason the gRPC C++ API's were not designed this way? Any history or context would help greatly!

Thanks,
Sumukh

yas...@google.com

unread,
Apr 22, 2019, 3:27:14 PM4/22/19
to grpc.io
Hi, we have recently added an Async Callback API version for C++. It is currently marked experimental but I believe it satisfies your requirements.

sumuk...@gmail.com

unread,
Apr 22, 2019, 3:32:27 PM4/22/19
to grpc.io
Thanks Yash, I'll take a look.
Reply all
Reply to author
Forward
0 new messages