Reputation: 97
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...
Upvotes: 0
Views: 145
Reputation: 182753
You must call pthread_cond_wait
when, and only when, you know there is something to wait for. You acquire the mutex so you can check shared state, but then you don't check anything. That can't possibly be right.
You must call pthread_cond_signal
when, and only when, you need to let another thread know that you have changed something. You acquire the mutex, call pthread_cond_signal
, and release the mutex. But you haven't changed anything another thread could possibly be waiting for. That can't possibly be right.
Bluntly, there's no evidence you understand how a condition variable works or how to use one properly. The mutex associated with the condition variable must protect the predicate, the thing you are waiting for. But I see no evidence of any predicate in your code, so there's nothing to wait for and nothing whose readiness needs to be signaled.
You can find a good explanation of the basics of using condition variables here. Some of the other answers are helpful too.
Assuming you wanted your threads to alternate, here's your code fixed:
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;
bool read_turn = true;
int A;
void * read_input(void* a)
{
int t;
cout<<" reading .... using thread1 "<<endl;
for(;;)
{
pthread_mutex_lock(&mutex_var);
// Wait our turn
while (!read_turn)
pthread_cond_wait ( &cond_var, &mutex_var );
cout<<" input value of A "<<endl;
cin >> A;
cout<<" A---> VALUE Changed to inputed one"<<endl;
// It's now the other thread's turn
read_turn = false;
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);
// Wait our turn
while (read_turn)
pthread_cond_wait( &cond_var ,&mutex_var );
cout<< " value of (A)= "<<A <<endl;
// Now it's the other guy's turn
read_turn = true;
pthread_cond_signal (& cond_var );
if(A==10)
{
pthread_mutex_unlock(&mutex_var);
pthread_exit(NULL);
}
pthread_mutex_unlock(&mutex_var);
}
}
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;
}
Notice that it waits when, and only when, it knows there is something to wait for. And notice that it signals when, and only when, it has changed some shared state that another thread might be waiting for.
Upvotes: 1