lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | /* Creates two threads, one printing 10000 "a"s, the other printing |
| 2 | 10000 "b"s. |
| 3 | Illustrates: thread creation, thread joining. */ |
| 4 | |
| 5 | #include <stddef.h> |
| 6 | #include <stdio.h> |
| 7 | #include <unistd.h> |
| 8 | #include "pthread.h" |
| 9 | |
| 10 | static void *process(void * arg) |
| 11 | { |
| 12 | int i; |
| 13 | printf("Starting process %s\n", (char *)arg); |
| 14 | for (i = 0; i < 10000; i++) |
| 15 | write(1, (char *) arg, 1); |
| 16 | return NULL; |
| 17 | } |
| 18 | |
| 19 | #define sucfail(r) (r != 0 ? "failed" : "succeeded") |
| 20 | int main(void) |
| 21 | { |
| 22 | int pret, ret = 0; |
| 23 | pthread_t th_a, th_b; |
| 24 | void *retval; |
| 25 | |
| 26 | ret += (pret = pthread_create(&th_a, NULL, process, (void *)"a")); |
| 27 | printf("create a %s %d\n", sucfail(pret), pret); |
| 28 | ret += (pret = pthread_create(&th_b, NULL, process, (void *)"b")); |
| 29 | printf("create b %s %d\n", sucfail(pret), pret); |
| 30 | ret += (pret = pthread_join(th_a, &retval)); |
| 31 | printf("join a %s %d\n", sucfail(pret), pret); |
| 32 | ret += (pret = pthread_join(th_b, &retval)); |
| 33 | printf("join b %s %d\n", sucfail(pret), pret); |
| 34 | return ret; |
| 35 | } |