int i; /* global variable */ pthread_mutex_t loopLock = PTHREAD_MUTEX_INITIALIZER; . . void fn( void ) { pthread_mutex_lock( &loopLock ); for( i = 0; i != 4; ++i ) { printf( "%d ", i ); } printf( "\n" ); pthread_mutex_unlock( &loopLock ); } . .