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;
}
在这里,我尝试使用 read_input 读取输入,然后使用 write_input 打印该值...
但它在 read_input 循环中继续,而不是控制 write_input... 这应该将互斥控制返回到 write_input 线程以打印值。请帮忙...