Add diep() function
[eleutheria.git] / pthreads / pthread_create.c
blobd154a1df981d0a74ebb345936b896445e18ea4c5
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <pthread.h>
5 #define NUM_THREADS 20
7 /* function prototypes */
8 void *threadfun(void *arg);
9 void diep(const char *s);
11 int main(void)
13 pthread_t tid[NUM_THREADS];
14 int cnt[NUM_THREADS], i;
16 for (i = 0; i < NUM_THREADS; i++) {
17 cnt[i] = i;
18 printf("Creating thread: %d\n", i);
19 if (pthread_create(&tid[i], NULL, threadfun, (void *)&cnt[i]))
20 diep("pthread_create() error\n");
23 /* make sure all threads are done */
24 for (i = 0; i < NUM_THREADS; i++)
25 if (pthread_join(tid[i], NULL))
26 perror("pthread_join");
28 return EXIT_SUCCESS;
31 void *threadfun(void *arg)
33 printf("Hello! I am thread: %d\n", *(int *) arg);
34 pthread_exit(NULL);
37 void diep(const char *s)
39 perror(s);
40 exit(EXIT_FAILURE);