/* System V Semaphores and pthreads. */ #include #include #include #include #include #include #include "nya/array.h" #define massert( cond, msg ) \ if( cond ) { \ perror( msg ); \ exit(-1); \ } #define SLEEP SLEEP char buf[] = "abcdefghijklmnopqrstuvwsyz"; pthread_mutex_t mut; void *inv( void *data ) { int len = strlen(buf); while( 1 ) { massert(pthread_mutex_lock(&mut) != 0, "pthread_mutex_lock"); inverse(buf, len); usleep( SLEEP ); massert(pthread_mutex_unlock(&mut) != 0, "pthread_mutex_unlock"); } } void *rev( void* data ) { int len = strlen(buf); while( 1 ) { massert(pthread_mutex_lock(&mut) != 0, "pthread_mutex_lock"); reverse(buf, len); usleep( SLEEP ); massert(pthread_mutex_unlock(&mut) != 0, "pthread_mutex_unlock"); } } int main( int argc, char* argv[] ) { pthread_t inv_pthread, rev_pthread; massert( pthread_mutex_init(&mut, NULL) != 0, "pthread_init_mutex"); massert(pthread_create( &inv_pthread, NULL, inv, NULL ) != 0, "pthread" ); massert(pthread_create( &rev_pthread, NULL, rev, NULL ) != 0, "pthread" ); while(1) { massert(pthread_mutex_lock(&mut) != 0, "pthread_mutex_lock"); puts(buf); massert(pthread_mutex_unlock(&mut) != 0, "pthread_mutex_unlock"); usleep( SLEEP ); } pthread_exit( NULL ); return 0; }