Dummy commit to test new ssh key
[eleutheria.git] / pthreads / pthread_create.c
blobcca2082f71daca1ed1a17acad2a64125fef2c703
1 /*
2 * Compile with:
3 * gcc pthread_create.c -o pthread_create -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[NUM_THREADS];
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);
24 if (pthread_create(&tid[i], NULL, threadfun, (void *)&cnt[i]))
25 diep("pthread_create() error\n");
28 /* Make sure all threads are done */
29 for (i = 0; i < NUM_THREADS; i++)
30 if (pthread_join(tid[i], NULL))
31 diep("pthread_join");
33 return EXIT_SUCCESS;
36 void *threadfun(void *arg)
38 printf("Hello! I am thread: %d\n", *(int *) arg);
39 pthread_exit(NULL);
42 void diep(const char *s)
44 perror(s);
45 exit(EXIT_FAILURE);