composition_4.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. * Copyright 2018-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <algorithm>
  17. #include <cassert>
  18. #include <iostream>
  19. #include <vector>
  20. #include <folly/experimental/pushmi/examples/pool.h>
  21. #include <folly/experimental/pushmi/strand.h>
  22. #include <folly/experimental/pushmi/o/request_via.h>
  23. #include <folly/experimental/pushmi/o/tap.h>
  24. #include <folly/experimental/pushmi/o/transform.h>
  25. using namespace pushmi::aliases;
  26. template <class Io>
  27. auto io_operation(Io io) {
  28. return io | op::transform([](auto) { return 42; }) |
  29. op::tap([](int v) { printf("io pool producing, %d\n", v); }) |
  30. op::request_via();
  31. }
  32. int main() {
  33. mi::pool cpuPool{std::max(1u, std::thread::hardware_concurrency())};
  34. mi::pool ioPool{std::max(1u, std::thread::hardware_concurrency())};
  35. auto io = ioPool.executor();
  36. auto cpu = cpuPool.executor();
  37. io_operation(io).via(mi::strands(cpu)) |
  38. op::tap([](int v) { printf("cpu pool processing, %d\n", v); }) |
  39. op::submit();
  40. // when the caller is not going to process the result (only side-effect
  41. // matters) or the caller is just going to push the result into a queue.
  42. // provide a way to skip the transition to a different executor and make it
  43. // stand out so that it has to be justified in code reviews.
  44. mi::via_cast<mi::is_sender<>>(io_operation(io)) | op::submit();
  45. ioPool.wait();
  46. cpuPool.wait();
  47. std::cout << "OK" << std::endl;
  48. }