21 std::cout <<
"[Producer] Starting production..." << std::endl;
24 for (
int i = 1; i <= 5; ++i) {
26 std::cout <<
"[Producer] Producing value: " <<
value_to_send_ << std::endl;
31 std::cout <<
"[Producer] Value sent, yielding..." << std::endl;
34 std::cout <<
"[Producer] Production complete!" << std::endl;
39 copp::stackful_channel_sender<int>
sender_;
48 std::cout <<
"[Consumer] Starting consumption..." << std::endl;
51 for (
int i = 1; i <= 5; ++i) {
52 std::cout <<
"[Consumer] Waiting for value " << i <<
"..." << std::endl;
55 int value = cotask::task<>::this_task()->await_value(
receiver_);
57 std::cout <<
"[Consumer] Received value: " << value << std::endl;
63 std::cout <<
"[Consumer] Consumption complete!" << std::endl;
72 std::cout <<
"\n========== Example 1: Basic Channel Usage ==========\n" << std::endl;
75 std::pair<copp::stackful_channel_receiver<int>, copp::stackful_channel_sender<int>> channel =
76 copp::make_stackful_channel<int>();
77 auto receiver = std::move(channel.first);
78 auto sender = std::move(channel.second);
81 auto consumer = cotask::task<>::create(
consumer_action(std::move(receiver)));
82 auto producer = cotask::task<>::create(
producer_action(std::move(sender)));
85 std::cout <<
"Starting consumer task..." << std::endl;
88 std::cout <<
"\nStarting producer task..." << std::endl;
91 std::cout <<
"\nBoth tasks completed!" << std::endl;
103 std::cout <<
"[Consumer] Starting with error handling..." << std::endl;
107 int value = cotask::task<>::this_task()->await_value(
receiver_);
108 std::cout <<
"[Consumer] Received: " << value << std::endl;
114 auto error_transform = [](copp::copp_error_code err) {
115 std::cout <<
"[Consumer] Error occurred: " <<
static_cast<int>(err) <<
", returning default value -1"
120 int value = cotask::task<>::this_task()->await_value(
receiver_, error_transform);
121 std::cout <<
"[Consumer] Received or defaulted: " << value << std::endl;
132 std::cout <<
"\n========== Example 2: Error Handling ==========\n" << std::endl;
134 std::pair<copp::stackful_channel_receiver<int>, copp::stackful_channel_sender<int>> channel =
135 copp::make_stackful_channel<int>();
136 auto receiver = std::move(channel.first);
137 auto sender = std::move(channel.second);
144 sender.set_value(42);
147 std::cout <<
"\nKilling consumer task to demonstrate error handling..." << std::endl;
150 std::cout <<
"\nTask killed!" << std::endl;
165 std::cout <<
"[Direct Consumer] Starting..." << std::endl;
168 auto error_transform = [](copp::copp_error_code err) {
169 std::cout <<
"[Direct Consumer] Error: " <<
static_cast<int>(err) << std::endl;
173 for (
int i = 0; i < 3; ++i) {
175 auto* ctx = copp::this_coroutine::get<copp::coroutine_context>();
177 std::cout <<
"[Direct Consumer] Awaiting value " << (i + 1) <<
"..." << std::endl;
180 int value = data->receiver.inject_await(ctx, error_transform);
182 std::cout <<
"[Direct Consumer] Received: " << value << std::endl;
183 data->received_sum += value;
185 data->receiver.reset_value();
188 std::cout <<
"[Direct Consumer] Total sum: " << data->received_sum << std::endl;
193 std::cout <<
"\n========== Example 4: Direct Coroutine Context Usage ==========\n" << std::endl;
195 std::pair<copp::stackful_channel_receiver<int>, copp::stackful_channel_sender<int>> channel =
196 copp::make_stackful_channel<int>();
197 auto receiver = std::move(channel.first);
198 auto sender = std::move(channel.second);
201 data.receiver = std::move(receiver);
204 typedef copp::coroutine_context_container<copp::allocator::stack_allocator_malloc> coroutine_t;
208 std::cout <<
"Starting direct coroutine..." << std::endl;
212 for (
int value : {10, 20, 30}) {
213 std::cout <<
"\nSending value: " << value << std::endl;
214 sender.set_value(value);
217 std::cout <<
"\nDirect coroutine completed with sum: " << data.received_sum << std::endl;
225#if defined(LIBCOTASK_MACRO_ENABLED)
230 std::cout <<
"\n========== All Examples Completed! ==========\n" << std::endl;
232 std::cerr <<
"libcotask is not enabled, sample is disabled" << std::endl;
consumer_action(copp::stackful_channel_receiver< int > receiver)
int operator()(void *) override
copp::stackful_channel_receiver< int > receiver_
consumer_with_error_handling(copp::stackful_channel_receiver< int > receiver)
int operator()(void *) override
copp::stackful_channel_receiver< int > receiver_
int operator()(void *) override
producer_action(copp::stackful_channel_sender< int > sender)
copp::stackful_channel_sender< int > sender_
int direct_consumer_runner(void *data_ptr)
void example_basic_channel()
void example_error_handling()
void example_direct_coroutine_context()
copp::stackful_channel_receiver< int > receiver