Rename semaphore.c to pthread_semaphore.c
[eleutheria.git] / pthread_create.c
blob7dd8a289fed1a09cbbfd6c20c65dd8502317dc3a
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);
10 int main() {
11 pthread_t tid[NUM_THREADS];
12 int i, cnt[NUM_THREADS];
14 for (i=0; i<NUM_THREADS; i++) {
15 cnt[i] = i;
16 printf("Creating thread: %d\n", i);
17 if (pthread_create(&tid[i], NULL, threadfun, (void *)&cnt[i])) {
18 fprintf(stderr, "pthread_create() error\n");
19 exit(EXIT_FAILURE);
23 /* make sure all threads are done */
24 for (i=0; i<NUM_THREADS; i++)
25 if (pthread_join(tid[i], NULL)) {
26 fprintf(stderr, "pthread_join() error\n");
27 exit(EXIT_FAILURE);
30 return EXIT_SUCCESS;
33 void *threadfun(void *arg) {
34 printf("Hello! I am thread: %d\n", *(int *) arg);
35 pthread_exit(NULL);