Lines Matching refs:semaphore

29 #include <semaphore.h>
36 /* a simple semaphore test, using three threads
38 * a semaphore is initialized with a value of 1
42 * Thread 1 takes the semaphore, then sleeps for 2 seconds, then post the semaphore
43 * Thread 2 sleeps for 1 second, then waits the semaphore, sleeps for 2 seconds, then post the semaphoe
44 * Thread 3 sleeps 3 seconds, waits for the semaphore
47 static sem_t semaphore;
52 printf( "thread 1: waiting for semaphore\n" );
53 if ( sem_wait( &semaphore ) < 0 ) {
54 printf( "thread 1: could not wait for semaphore: %s\n", strerror(errno) );
57 printf( "thread 1: got the semaphore ! sleeping for 2 seconds\n" );
59 printf( "thread 1: awake !! posting semaphore\n" );
60 if ( sem_post( &semaphore ) < 0 ) {
61 printf( "thread 2: could not post semaphore: %s\n", strerror(errno) );
72 printf( "thread 2: awake !! waiting for semaphore\n" );
73 if ( sem_wait( &semaphore ) < 0 ) {
74 printf( "thread 2: could not wait for semaphore: %s\n", strerror(errno) );
77 printf( "thread 2: got the semaphore ! sleeping for 2 seconds\n" );
79 printf( "thread 2: awake !! posting semaphore\n" );
80 if ( sem_post( &semaphore ) < 0 ) {
81 printf( "thread 2: could not post semaphore: %s\n", strerror(errno) );
93 printf( "thread 3: awake !! waiting for semaphore\n" );
94 if ( sem_wait( &semaphore ) < 0 ) {
95 printf( "thread 3: could not wait for semaphore: %s\n", strerror(errno) );
98 printf( "thread 3: got semaphore. quitting\n" );
116 if ( sem_init( &semaphore, 0, 1 ) < 0 ) {
117 printf( "could not initialize semaphore: %s\n", strerror(errno) );