crossbario / autobahn-cpp

WAMP for C++ in Boost/Asio
https://crossbar.io/autobahn
Boost Software License 1.0
251 stars 104 forks source link

Create single-threaded concurrent caller example #204

Open oberstet opened 4 years ago

oberstet commented 4 years ago

Add an example:

The 5 calls should run concurrently (the sum of all 5 results is printed after <=5s) and the caller (and callee) must be single-threaded.

To dispatch a future on the main/current thread:

boost::launch::deferred

see https://github.com/crossbario/autobahn-cpp/blob/master/examples/callee_new.cpp#L92 and eg https://stackoverflow.com/questions/25327256/execute-in-main-thread-with-boostfuturethen

To get the thread id:

boost::this_thread::get_id()

To wait for a set of futures:

boost::wait_for_all(f1, f2, f3, f4, f5);

see https://stackoverflow.com/questions/14170287/future-composability-and-boostwait-for-all

i94matjo commented 4 years ago

After some digging into boost(!) the exact same functionality as suggested in #203 can be accomplished by defining BOOST_THREAD_PROVIDES_EXECUTORS and use boost::inline_executor as executor on the future continuations. So #203 is totally unnecessary, Well, you live and learn... If that's the way we end up using remains to be seen...

oberstet commented 4 years ago

interesting! thanks for dropping this info here.

yeah, these features still seem to be very newish and lacking in docs. historically (when we started autobahnc++), even mere futures in boost were mildly problematic;) things have improved these days ..

anyways, fwiw, besides the boost implementation of executors, there seems to be https://github.com/chriskohlhoff/executors (I haven't used it). but in general, yep, that executors c++23 feature looks promising .. eg all this stuff https://cor3ntin.github.io/posts/executors/

note: executors are not needed to implement the example as described in the issue here