modified: SpatialOmicsCoord.py
[GalaxyCodeBases.git] / c_cpp / pthreadtest / 03semaphoreexample.c
blob7453e255e8254815e646a0017e64199620f98faf
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 */
6 #include <pthread.h>
7 #include <semaphore.h>
8 #include <stdio.h>
10 void * simple1(void *);
11 void * simple2(void *);
13 #define NUM_THREADS 2
14 pthread_t tid[NUM_THREADS]; /* array of thread IDs */
15 sem_t semA, semB;
17 main( int argc, char *argv[] )
19 int i, ret;
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);
31 } /* main */
35 void * simple1(void * parm)
37 printf("Thread 1 here, before the barrier.\n");
38 sem_post(&semB);
39 sem_wait(&semA);
40 printf("Thread 1 after the barrier.\n");
43 void * simple2(void * parm)
45 printf("Thread 2 here, before the barrier.\n");
46 sem_post(&semA);
47 sem_wait(&semB);
48 printf("Thread 2 after the barrier.\n");