#include #include #include void *do_work(void *thr); int counter = 0; pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER; main(){ pthread_t thread1; pthread_t thread2; char *thread1_identity = "Thread A: "; char *thread2_identity = "Thread B: "; int ret1 = pthread_create( &thread1, NULL, do_work, (void*)thread1_identity); int ret2 = pthread_create( &thread2, NULL, do_work, (void*)thread2_identity); pthread_join( thread1, NULL); pthread_join( thread2, NULL); exit(0); } void *do_work(void *thr) { char *thread_identity = (char*)thr; int HALT = 10; while( counter < HALT ) { printf( "%s %d\n", thread_identity, counter ); pthread_mutex_lock( &mtx); counter++; pthread_mutex_unlock( &mtx); sleep( 1 ); } }