| 1 | #include <pthread.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <stdio.h> |
| 4 | |
| 5 | const int N = 1000; |
| 6 | void *x[N]; |
| 7 | |
| 8 | void *Thread1(void *unused) { |
| 9 | for (int i = 0; i < N; i++) { |
| 10 | fprintf(stderr, format: "%s %d\n" , __func__, i); |
| 11 | free(ptr: x[i]); |
| 12 | } |
| 13 | return NULL; |
| 14 | } |
| 15 | |
| 16 | void *Thread2(void *unused) { |
| 17 | for (int i = 0; i < N; i++) { |
| 18 | fprintf(stderr, format: "%s %d\n" , __func__, i); |
| 19 | free(ptr: x[i]); |
| 20 | } |
| 21 | return NULL; |
| 22 | } |
| 23 | |
| 24 | int main() { |
| 25 | for (int i = 0; i < N; i++) |
| 26 | x[i] = malloc(size: 128); |
| 27 | pthread_t t[2]; |
| 28 | pthread_create(newthread: &t[0], attr: 0, start_routine: Thread1, arg: 0); |
| 29 | pthread_create(newthread: &t[1], attr: 0, start_routine: Thread2, arg: 0); |
| 30 | pthread_join(th: t[0], thread_return: 0); |
| 31 | pthread_join(th: t[1], thread_return: 0); |
| 32 | } |
| 33 | |