I finally go this to work. Part of the problem was using the the globally defined variable i, and the mechanism used to advance the counter turn in the function thread_func. Here is the reworked code for the function thread_func.
void *thread_func( void *arg ) {
int id = ((int *)arg)[0]; // this thread's identifier
int loop;
for ( loop = 0; loop < 10; loop++ ) { // repeat 10 times
// enter the critical section
//<You must insert code here>
pthread_mutex_lock(&mutex);
while ( turn != id ) {
// wait until the (id - 1)th thread signals me.
pthread_cond_wait(&cond[id],&mutex);
}
if (id==0) printf("************** starting turn %d ***************\n",loop);
printf( "thread[%d] got %dth turn \n", id, loop)
// signal the next thread
// leave the critical section
//<You must insert code here>
turn = (id + 1) % nThreads;
pthread_cond_signal(&cond[turn]);
pthread_mutex_unlock(&mutex);
}
}