proxygen
composition_4.cpp
Go to the documentation of this file.
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 
22 
24 
26 
29 
30 using namespace folly::pushmi::aliases;
31 
32 template <class Io>
33 auto io_operation(Io io) {
34  return io | op::transform([](auto) { return 42; }) |
35  op::tap([](int v) { printf("io pool producing, %d\n", v); }) |
37 }
38 
39 int main() {
40  mi::pool cpuPool{std::max(1u, std::thread::hardware_concurrency())};
41  mi::pool ioPool{std::max(1u, std::thread::hardware_concurrency())};
42 
43  auto io = ioPool.executor();
44  auto cpu = cpuPool.executor();
45 
46  io_operation(io).via(mi::strands(cpu)) |
47  op::tap([](int v) { printf("cpu pool processing, %d\n", v); }) |
48  op::submit();
49 
50  // when the caller is not going to process the result (only side-effect
51  // matters) or the caller is just going to push the result into a queue.
52  // provide a way to skip the transition to a different executor and make it
53  // stand out so that it has to be justified in code reviews.
55 
56  ioPool.wait();
57  cpuPool.wait();
58 
59  std::cout << "OK" << std::endl;
60 }
auto io_operation(Io io)
requires Invocable< ExecutorFactory & > &&Executor< invoke_result_t< ExecutorFactory & > > &&ConcurrentSequence< invoke_result_t< ExecutorFactory & > > auto strands(ExecutorFactory ef)
Definition: strand.h:246
LogLevel max
Definition: LogLevel.cpp:31
PUSHMI_INLINE_VAR constexpr detail::tap_fn tap
Definition: tap.h:126
int main()
PUSHMI_INLINE_VAR constexpr detail::request_via_fn request_via
Definition: request_via.h:61
requires Same< To, is_sender<> > &&Sender< In > auto via_cast(In in)
Definition: request_via.h:67
PUSHMI_INLINE_VAR constexpr detail::transform_fn transform
Definition: transform.h:158