libcopp 2.3.1
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
sample_readme_11.cpp
Go to the documentation of this file.
1/*
2 * sample_readme_11.cpp
3 *
4 * Created on: 2025-03-04
5 * Author: owent
6 *
7 * Released under the MIT license
8 */
9
10#include <assert.h>
11#include <iostream>
12#include <string>
13
14// include manager header file
17
18#if defined(LIBCOPP_MACRO_ENABLE_STD_COROUTINE) && LIBCOPP_MACRO_ENABLE_STD_COROUTINE
19
20using my_receiver = copp::generator_channel_receiver<int>;
21using my_sender = copp::generator_channel_sender<int>;
22std::list<my_sender> g_sample_executor;
23
24static my_receiver generator_pick_receiver(std::pair<my_receiver, my_sender>&& receiver_and_sender) {
25 g_sample_executor.emplace_back(std::move(receiver_and_sender.second));
26 return receiver_and_sender.first;
27}
28
29static copp::callable_future<void> coroutine_simulator_rpc() {
30 my_receiver my_generator = generator_pick_receiver(copp::make_channel<int>());
31
32 auto value1 = co_await my_generator;
33 std::cout << "co_await named channel receiver: " << value1 << std::endl;
34 auto value2 = co_await generator_pick_receiver(copp::make_channel<int>());
35 std::cout << "co_await temporary channel receiver: " << value2 << std::endl;
36
37 co_return;
38}
39
40int main() {
41 int result = 191;
42 auto f = coroutine_simulator_rpc();
43
44 while (!g_sample_executor.empty()) {
45 auto ctx = g_sample_executor.front();
46 g_sample_executor.pop_front();
47 ctx->set_value(++result);
48 }
49 return 0;
50}
51#else
52int main() {
53 puts("this sample require cotask enabled and compiler support c++20 coroutine");
54 return 0;
55}
56#endif
int main()