6 * antithread - Accelerated Native Technology Implementation of "threads"
8 * On systems with multiple CPUs, it's often faster to split work across
9 * different execution units. Under Unix-like systems, the two methods of
10 * doing this are (POSIX) threads or processes.
12 * Threads have the disadvantage that they share all of the address space:
13 * using software instead of hardware isolation (eg. for malloc) is
14 * inefficient and less secure. Various subtle errors can occur because
15 * programmers in one part of the code do not expect concurrency.
17 * Processes have the disadvantage that there is no common infrastructure
18 * for sharing memory: without this, programmers are faced with the unpalatable
19 * options of using slower options or creating their own infrastructure.
21 * The antithread module provides memory-sharing infrastructure: the programmer
22 * indicates the size of the memory to share, and then creates subprocesses
23 * which share the memory. Pipes are used to hand pointers between the
24 * main process and the children: usually pointers into the shared memory.
27 * #include <ccan/antithread/antithread.h>
28 * #include <ccan/talloc/talloc.h>
34 * // Silly example: child makes rot13 copy.
35 * static void *rot13(struct at_pool *pool, void *unused)
38 * while ((r = at_read_parent(pool)) != NULL) {
40 * // r is inside pool, so talloc off it is also inside.
41 * p = talloc_array(r, char, strlen(r) + 1);
42 * for (i = 0; r[i]; i++) {
45 * else if (toupper(r[i]) < 'N')
50 * // Tell parent about our copy.
51 * at_tell_parent(pool, p);
56 * #define NUM_CHILDREN 4
58 * int main(int argc, char *argv[])
60 * struct at_pool *pool;
61 * struct athread *child[NUM_CHILDREN];
64 * // Create pool and some children
65 * pool = at_pool(1024*1024);
66 * for (i = 0; i < NUM_CHILDREN; i++)
67 * child[i] = at_run(pool, rot13, NULL);
69 * // Pass out work to children.
70 * for (i = 1; i < argc; i++)
71 * at_tell(child[i % NUM_CHILDREN],
72 * talloc_strdup(at_pool_ctx(pool), argv[i]));
74 * // Read back results.
75 * for (i = 1; i < argc; i++)
76 * printf("%s ", (char *)at_read(child[i % NUM_CHILDREN]));
79 * // Freeing pool kills children, too.
84 * License: GPL (v3 or any later version)
85 * Author: Rusty Russell <rusty@rustcorp.com.au>
87 int main(int argc, char *argv[])
92 if (strcmp(argv[1], "depends") == 0) {
93 printf("ccan/antithread/alloc\n");
95 printf("ccan/list\n");
96 printf("ccan/noerr\n");
97 printf("ccan/read_write_all\n"); /* For tests */
98 printf("ccan/talloc\n");
99 printf("ccan/typesafe_cb\n");