aboutsummaryrefslogtreecommitdiff
path: root/example/simple.c
diff options
context:
space:
mode:
Diffstat (limited to 'example/simple.c')
-rw-r--r--example/simple.c57
1 files changed, 57 insertions, 0 deletions
diff --git a/example/simple.c b/example/simple.c
new file mode 100644
index 0000000..3d4df1a
--- /dev/null
+++ b/example/simple.c
@@ -0,0 +1,57 @@
+/*
+ * Compile with
+ * gcc -o simple simple.c -ltuberia -lpthread
+ * or
+ * gcc -o simple simple.c `pkg-config --cflags --libs --static tuberia`
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <tuberia.h>
+
+void *stage_one(void *element, void *opaque)
+{
+ (void)opaque;
+ int *i = element;
+ *i += 5;
+ return i;
+}
+
+void *stage_two(void *element, void *opaque)
+{
+ (void)opaque;
+ int *i = element;
+ *i *= 10;
+ return i;
+}
+
+void sink(void *element, void *opaque)
+{
+ (void)opaque;
+ int *i = element;
+ printf("sink: %d\n", *i);
+ free(i);
+}
+
+int main(void)
+{
+ tube *ctx;
+ tube_source *source;
+ int *element, n;
+
+ source = tube_source_alloc(2, NULL, NULL, free);
+ tube_stage_append(source, tube_stage_alloc(2, stage_one, NULL, free));
+ tube_stage_append(source, tube_stage_alloc(2, stage_two, NULL, free));
+ ctx = tube_alloc(source, sink, NULL);
+ tube_source_and_stages_free(&source);
+ tube_start(ctx);
+ while (scanf("%d", &n) == 1) {
+ element = malloc(sizeof(*element));
+ *element = n;
+ tube_inject(ctx, 1000, element);
+ }
+ tube_free(&ctx);
+
+ return 0;
+}
+