Skip to content

Instantly share code, notes, and snippets.

@weissi
Created February 4, 2016 16:46
Show Gist options
  • Save weissi/fa6d59ea3e71d276b7c5 to your computer and use it in GitHub Desktop.
Save weissi/fa6d59ea3e71d276b7c5 to your computer and use it in GitHub Desktop.
#include <dispatch/dispatch.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
int main()
{
int pipes[2] = {0, 0};
int err = pipe(pipes);
if (err) {
perror("pipe");
return 1;
}
fprintf(stderr, "=== BEGIN ===\n");
dispatch_queue_t q = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_io_t chan = dispatch_io_create(DISPATCH_IO_STREAM,
pipes[1],
q,
^(int error) {
fprintf(stderr, "ERROR: %d\n", error);
});
dispatch_io_write(chan,
0,
dispatch_data_create("hallo\n", 6, q, NULL),
q,
^(bool done, dispatch_data_t data, int error) {
if (!error) {
fprintf(stderr, "OK: %d, %p, %d\n", done, data, error);
} else {
fprintf(stderr, "ERROR: %d, %p, %d\n", done, data, error);
}
});
char buf[7];
read(pipes[0], buf, 6);
buf[6] = 0;
fprintf(stderr, "buf='%s'\n", buf);
dispatch_sync(q, ^{ fprintf(stderr, "on it\n"); });
dispatch_io_close(chan, DISPATCH_IO_STOP);
atexit_b(^{
fprintf(stderr, "=== END ===\n");
});
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment