#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/select.h>

typedef struct _pipe_backed_thing_t {
	int pipes[2];
	char data[8192];
} pipe_backed_thing_t;

void *routine(void *arg) {
	pipe_backed_thing_t *borrowed =
		(pipe_backed_thing_t *) arg;

	/* link does thing */
	memcpy(borrowed->data, "hello world", sizeof("hello world") - 1);

	/* if channel_is_observed_for_example() */
	write(borrowed->pipes[1],
		"\0", /* dummy packet null char */
		sizeof(char));

	return NULL;
}

int main(int argc, char** argv) {
	pipe_backed_thing_t thing;

	if (0 != pipe(thing.pipes)) {
		exit(1);
	}
	/* 0 = read end, 1 = write end */

	pthread_t thread;

	if (0 != pthread_create(&thread, NULL, routine, (void*)&thing)) {
		exit(1);
	}

	fd_set readfds;
	int rfd = thing.pipes[0];

	FD_ZERO(&readfds);
	FD_SET(rfd, &readfds);

	int ready = select(rfd + 1, &readfds, NULL, NULL, NULL);

	if (ready == -1) {
		perror("select");
		exit(1);
	}

	if (FD_ISSET(rfd, &readfds)) {
		char dummy;
		read(rfd, &dummy, sizeof(dummy));
		printf("got signal, data = %s\n", thing.data);
	}

	pthread_join(thread, NULL);

	close(thing.pipes[0]);
	close(thing.pipes[1]);

	return 0;
}