extern "C"
{
#include<pthread.h>
}
#include<iostream>
using namespace std;
pthread_mutex_t mutex_var = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond_var= PTHREAD_COND_INITIALIZER;
int A;
void * read_input(void* a)
{
int t;
cout<<" reading .... using thread1 "<<endl;
for(;;)
{
pthread_mutex_lock(&mutex_var);
cout<<" input value of A "<<endl;
cin >> A;
cout<<" A---> VALUE Changed to inputed one"<<endl;
t=pthread_cond_signal( &cond_var );
if(t==0)
{
cout<<"logsss"<<endl;
}
pthread_mutex_unlock(&mutex_var);
// pthread_cond_signal( &cond_var );
if(A==10)
{
pthread_exit(NULL);
}
}
}
void * write_input( void * b)
{
cout<<" writing .... using thread2 "<<endl;
for(;;)
{
pthread_mutex_lock(&mutex_var);
pthread_cond_wait( &cond_var ,&mutex_var );
cout<< " value of (A)= "<<A <<endl;
pthread_mutex_unlock(&mutex_var);
if(A==10)
{
pthread_exit(NULL);
}
}
}
int main()
{
pthread_t r,w;
pthread_create(&w,NULL,&write_input,NULL);
pthread_create(&r,NULL,&read_input,NULL);
pthread_join(w,NULL);
pthread_join(r,NULL);
return 0;
}
here I am trying to read a input using read_input and then print that value using write_input...
but it goes on in read_input loop and not giving control to write_input... which should retrun mutex control to write_input thread to print the value. please help...