-
Notifications
You must be signed in to change notification settings - Fork 278
how to get synchronous send ? how to wait() on async call ? #72
Comments
Thank you for starting this discussion, it is very interesting. First off, Secondly, the PPL Tasks are interesting, and if I remember correctly they are proposed for the C++ standard, but will not be accepted before earliest C++20. The main problem here is that using PPL Tasks will greatly affect how you write your program. You can't for instance call With respect to waiting (in a nonblocking manner) for async functions, the boost way is to use Finally, we have our current solution, that has none of the above drawbacks, but with the drawback of potentially leading to many nested callbacks. There is no perfect solution in my opinion, but will be following the c++ standard committee's work on asio and related additions. |
JavaScript's Promise is actually my favourite way of chaining async calls, but to my knowledge this is currently not possible to do with c++'s (boost::)asio. One problem I guess would be to keep the promise/future objects alive when leaving scope. |
How about this?
|
In http://www.boost.org/doc/libs/1_64_0/doc/html/thread/synchronization.html#thread.synchronization.futures.reference.unique_future.then you have the line (under Notes): I'll look into this further in the following days. |
relavant to what you just said:
|
In your above link, synchronous_job::run is blocking. Our case is a bit different than the one described in the link, since both the promise/future and the callback should run in the same thread, for instance in the event loop (which is the default threading strategy in Simple-Web(Socket)-Server). Additionally, the promise/future should be kept alive without returning the future (as in JavaScript where the promises are kept alive through garbage collection I guess). I think PPL Tasks solves this by always returning the tasks until eventually a wait() (or something similar) is called on the task chain. |
I was a bit unclear above, what I mean by running in the same thread, is that edit: changed |
@eidheim
cpprest library allows
then
andwait
, eg:client.send(out_msg).wait();
(see https://stackoverflow.com/questions/34423092/websocket-library)Likewise, beast has a synchronous interface (http://vinniefalco.github.io/beast/beast/design/websocket_zaphoyd.html)
How can I call
wait
after asend
(and avoid the "callback hell" pattern (https://colintoh.com/blog/staying-sane-with-asynchronous-programming-promises-and-generators) that you suggested in https://github.com/eidheim/Simple-WebSocket-Server/blob/master/ws_examples.cpp#L79 in response to #24 ?EDIT: this seems very relevant:
https://stackoverflow.com/questions/20709725/how-to-wait-for-an-asio-handler
The text was updated successfully, but these errors were encountered: