51 lines · c
1#include <pthread.h>2#include <stdio.h>3#include <stdlib.h>4#include <unistd.h>5 6char *pointer;7 8void *nothing(void *p) {9 return NULL;10}11 12void *f1(void *p) {13 pointer[0] = 'x';14 sleep(100);15 return NULL;16}17 18void *f2(void *p) {19 pointer[0] = 'y';20 sleep(100);21 return NULL;22}23 24int main (int argc, char const *argv[])25{26 pointer = (char *)malloc(10);27 28 for (int i = 0; i < 3; i++) {29 pthread_t t;30 pthread_create(&t, NULL, nothing, NULL);31 pthread_join(t, NULL);32 }33 34 pthread_t t1;35 pthread_create(&t1, NULL, f1, NULL);36 37 for (int i = 0; i < 3; i++) {38 pthread_t t;39 pthread_create(&t, NULL, nothing, NULL);40 pthread_join(t, NULL);41 }42 43 pthread_t t2;44 pthread_create(&t2, NULL, f2, NULL);45 46 pthread_join(t1, NULL);47 pthread_join(t2, NULL);48 49 return 0;50}51