1 /* Simple pthread example with semaphores */
2 /* To compile me for Linux, type: */
3 /* gcc -o filename filename.c -lpthread */
4 /* To execute, type: filename */
10 void * simple1(void *);
11 void * simple2(void *);
14 pthread_t tid
[NUM_THREADS
]; /* array of thread IDs */
17 main( int argc
, char *argv
[] )
21 sem_init(&semA
, 0, 0);
22 sem_init(&semB
, 0, 0);
24 pthread_create(&tid
[0], NULL
, simple1
, NULL
);
25 pthread_create(&tid
[1], NULL
, simple2
, NULL
);
26 for ( i
= 0; i
< NUM_THREADS
; i
++)
27 pthread_join(tid
[i
], NULL
);
29 printf("\nmain() reporting that all %d threads have terminated\n", i
);
35 void * simple1(void * parm
)
37 printf("Thread 1 here, before the barrier.\n");
40 printf("Thread 1 after the barrier.\n");
43 void * simple2(void * parm
)
45 printf("Thread 2 here, before the barrier.\n");
48 printf("Thread 2 after the barrier.\n");