pthreads changed
[eleutheria.git] / pthreads / pthread_join.c
blob0f6cb8cc54dbb0256a3db829f6673c9eafb2e405
1 /*
2 * Compile with:
3 * gcc pthread_join.c -o pthread_join -lpthread -Wall -W -Wextra -ansi -pedantic
4 */
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <pthread.h>
10 #define NUM_THREADS 20
12 /* function prototypes */
13 void *threadfun(void *arg);
14 void diep(const char *s);
16 int main(void)
18 pthread_t tid;
19 int cnt[NUM_THREADS], i;
21 for (i = 0; i < NUM_THREADS; i++) {
22 cnt[i] = i;
23 printf("Creating thread: %d\n", i);
25 if (pthread_create(&tid, NULL, threadfun, (void *)&cnt[i]))
26 diep("pthread_create");
28 if (pthread_join(tid, NULL))
29 diep("pthread_join()");
32 return EXIT_SUCCESS;
35 void *threadfun(void *arg)
37 printf("Hello! I am thread: %d\n", *(int *) arg);
38 pthread_exit(NULL);
41 void diep(const char *s)
43 perror(s);
44 exit(EXIT_FAILURE);