Merge branch 'exp-hash'
[eleutheria.git] / pthreads / pthread_join.c
blobeca97cf5cbc3b604e72566aa36782145182058db
1 /* compile with:
2 gcc pthread_join.c -o pthread_join -lpthread -Wall -W -Wextra -ansi -pedantic */
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <pthread.h>
8 #define NUM_THREADS 20
10 /* function prototypes */
11 void *threadfun(void *arg);
12 void diep(const char *s);
14 int main(void)
16 pthread_t tid;
17 int cnt[NUM_THREADS], i;
19 for (i = 0; i < NUM_THREADS; i++) {
20 cnt[i] = i;
21 printf("Creating thread: %d\n", i);
23 if (pthread_create(&tid, NULL, threadfun, (void *)&cnt[i]))
24 diep("pthread_create");
26 if (pthread_join(tid, NULL))
27 diep("pthread_join()");
30 return EXIT_SUCCESS;
33 void *threadfun(void *arg)
35 printf("Hello! I am thread: %d\n", *(int *) arg);
36 pthread_exit(NULL);
39 void diep(const char *s)
41 perror(s);
42 exit(EXIT_FAILURE);