summaryrefslogtreecommitdiffhomepage
path: root/misc/examples/coroutines/cotasks2.c
diff options
context:
space:
mode:
authortylov <[email protected]>2023-07-23 23:51:02 +0200
committertylov <[email protected]>2023-07-23 23:51:02 +0200
commitdd87a488eeeffa41a06757dda2220da21337a35e (patch)
treef9af2908c4f24775c57e87af51ed5dd08974cc0c /misc/examples/coroutines/cotasks2.c
parente8aed0431140fd0d202d302f19f756046858bad7 (diff)
downloadSTC-modified-dd87a488eeeffa41a06757dda2220da21337a35e.tar.gz
STC-modified-dd87a488eeeffa41a06757dda2220da21337a35e.zip
- algo/sort.h: Use plural form of i_key (or i_val) to define default name for sort, like: <i_key>s_sort_n(data, n).
- Updated some examples.
Diffstat (limited to 'misc/examples/coroutines/cotasks2.c')
-rw-r--r--misc/examples/coroutines/cotasks2.c35
1 files changed, 15 insertions, 20 deletions
diff --git a/misc/examples/coroutines/cotasks2.c b/misc/examples/coroutines/cotasks2.c
index 24a9f23f..4fdf98d9 100644
--- a/misc/examples/coroutines/cotasks2.c
+++ b/misc/examples/coroutines/cotasks2.c
@@ -42,18 +42,20 @@ int produce_items(struct produce_items* p, cco_runtime* rt)
{
cco_routine (p) {
p->str = cstr_null;
+ p->next.cco_func = next_value;
while (true)
{
- // await for next CCO_YIELD in next_value()
+ // await for next CCO_YIELD (or CCO_DONE) in next_value
cco_task_await(&p->next, rt, CCO_YIELD);
cstr_printf(&p->str, "item %d", p->next.val);
print_time();
printf("produced %s\n", cstr_str(&p->str));
cco_yield();
}
+
cco_cleanup:
- cstr_drop(&p->str);
- puts("done produce");
+ cstr_drop(&p->str);
+ puts("done produce");
}
return 0;
}
@@ -68,6 +70,8 @@ cco_task_struct (consume_items,
int consume_items(struct consume_items* c, cco_runtime* rt)
{
cco_routine (c) {
+ c->produce.cco_func = produce_items;
+
for (c->i = 1; c->i <= c->n; ++c->i)
{
printf("consume #%d\n", c->i);
@@ -75,10 +79,11 @@ int consume_items(struct consume_items* c, cco_runtime* rt)
print_time();
printf("consumed %s\n", cstr_str(&c->produce.str));
}
+
cco_cleanup:
- cco_stop(&c->produce);
- cco_task_resume(&c->produce, rt);
- puts("done consume");
+ cco_stop(&c->produce);
+ cco_task_resume(&c->produce, rt);
+ puts("done consume");
}
return 0;
}
@@ -86,18 +91,8 @@ int consume_items(struct consume_items* c, cco_runtime* rt)
int main(void)
{
struct consume_items consume = {
- .n=5,
- .cco_func=consume_items,
- .produce={.cco_func=produce_items, .next={.cco_func=next_value}},
+ .cco_func = consume_items,
+ .n = 5,
};
- int count = 0;
-
- cco_task_blocking(&consume)
- {
- ++count;
- //cco_sleep(0.001);
- //if (consume.i == 3)
- // cco_stop(&consume);
- }
- printf("count: %d\n", count);
-} \ No newline at end of file
+ cco_task_blocking(&consume);
+}