Cosmetic changes
[eleutheria.git] / pthreads / pthread_create.c
blob30e1e45f7614385fe2d85c7f4ea129d027b97f75
1 /* compile with:
2 gcc pthread_create.c -o pthread_create -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[NUM_THREADS];
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);
22 if (pthread_create(&tid[i], NULL, threadfun, (void *)&cnt[i]))
23 diep("pthread_create() error\n");
26 /* make sure all threads are done */
27 for (i = 0; i < NUM_THREADS; i++)
28 if (pthread_join(tid[i], NULL))
29 diep("pthread_join");
31 return EXIT_SUCCESS;
34 void *threadfun(void *arg)
36 printf("Hello! I am thread: %d\n", *(int *) arg);
37 pthread_exit(NULL);
40 void diep(const char *s)
42 perror(s);
43 exit(EXIT_FAILURE);