Subj : pthreads:same thread taking the control To : comp.programming.threads From : prasi Date : Mon Sep 26 2005 12:56 am hi, all I am new to threads,now I am learning pthreads.I have implemented a simple threaded program to check out condition variables in threads. The code is as follows #include #include /*this program creates two threads one thread will read the data from the keyboard stores in the buffer and another will read from the buffer and displays on the monitor.I am using pthread_cond_wait() and signal() for synchronization*/ //mutex to handle the shared data between read and write pthread_mutex_t shared_data; //condition variable pthread_cond_t got_data; char *buffer; pthread_t thread_id[10]; int num_bytes_written=0; void * read_sh_write_mon(void *arg) { char str[100]; //bytes_to_read=*(int *)arg; while(1) { pthread_mutex_lock(&shared_data); printf("write: num of bytes written=%d\n",num_bytes_written); pthread_cond_wait(&got_data,&shared_data); printf("write mon\n"); write(1,buffer,num_bytes_written); pthread_mutex_unlock(&shared_data); } } void * read_kb_write_sh(void *arg) { int bytes; while(1) { pthread_mutex_lock(&shared_data); printf("read\n"); bytes=read(0,buffer,1024); num_bytes_written=bytes; pthread_cond_signal(&got_data); printf("signal to write:num of bytes:%d\n",num_bytes_written); pthread_mutex_unlock(&shared_data); printf("unlock\n"); } } int main() { buffer=(char *)malloc(1024); pthread_mutex_init(&shared_data, NULL); pthread_cond_init (&got_data, NULL); pthread_create(&thread_id[0],NULL,read_sh_write_mon,NULL); pthread_create(&thread_id[1],NULL,read_kb_write_sh,NULL); printf("%d:\n",getpid()); for(;;) { printf( "server is running\n" ); pthread_yield(); sleep(8); } } " The problem is , the control is not transferring to the write thread ,the read thread is always getting the control and simply wait for accepting the data from the keyboard" Please mail me the reason if anybody knows. Thanks for ur precious time bye Prasanna .