Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create single-threaded concurrent caller example #204

Open
oberstet opened this issue Jan 24, 2020 · 2 comments
Open

Create single-threaded concurrent caller example #204

oberstet opened this issue Jan 24, 2020 · 2 comments

Comments

@oberstet
Copy link
Contributor

oberstet commented Jan 24, 2020

Add an example:

  • a callee that registers a "slow_square" procedure, waiting for a random time [0-5s]
  • a caller that issues 5 calls to ^ and waits_for_all 5 calls to return, and prints all 5 results and the sum

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
Copy link
Contributor

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
Copy link
Contributor Author

oberstet commented Jan 27, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants