首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 操作系统 > UNIXLINUX >

linux 线程编程解决方案

2012-05-15 
linux 线程编程求高手找错!!!输出时只有“Thread 1 createdThread2 created”#include stdio.h#include p

linux 线程编程
求高手找错!!!
输出时只有“Thread 1 created Thread2 created”





#include <stdio.h>
#include <pthread.h>
#define MAX 100

int buf1[MAX];
int buf2[MAX];
pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond=PTHREAD_COND_INITIALIZER;
pthread_cond_t cond1=PTHREAD_COND_INITIALIZER;
int size=0;

void readData1(void){
pthread_mutex_lock(&mutex);
if(buf1[0]!=0)
pthread_cond_wait(&cond,&mutex);
else{
FILE fp;
fp=fopen("1.dat","r");
while(!feof(fp)){
fscanf(fp,"%d",&buf1[size]);
++size;
}
fclose(fp);
printf("Thread1 created!\n");
pthread_cond_signal(&cond);
}
pthread_mutex_unlock(&mutex);
}

void readData2(void){
pthread_mutex_lock(&mutex);
if(buf2[0]!=0)
pthread_cond_wait(&cond1,&mutex);
else{
FILE fp;
fp=fopen("2.dat","r");
while(!feof(fp)){
fscanf(fp,"%d",&buf2[size]);
++size;
}
fclose(fp);
printf("Thread2 created!\n");
pthread_cond_signal(&cond1);
}
pthread_mutex_unlock(&mutex);
}

void process(void){
pthread_mutex_lock(&mutex);
if(buf1[0]==0){
pthread_cond_wait(&cond,&mutex);
}
else if(buf2[0]==0){
pthread_cond_wait(&cond1,&mutex);
}
while(1){
printf("Add:%d+%d=%d\n",buf1[size],buf2[size],buf1[size]+buf2[size]);
printf("Multiply:%d%d=%d\n",buf1[size],buf2[size],buf1[size]buf2[size]);
--size;
}
printf("Thread3 created!\n");
pthread_mutex_unlock(&mutex);
}

int main()
{
pthread_t t1,t2,t3;
pthread_create(&t1,NULL,(void*)readData1,NULL);
pthread_create(&t2,NULL,(void*)readData2,NULL);
pthread_create(&t3,NULL,(void*)process,NULL);
pthread_join(t1,NULL);
pthread_join(t2,NULL);
pthread_join(t3,NULL);
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);
return 0;
}






[解决办法]
生产者/消费者

C/C++ code
#include <stdio.h>#include <pthread.h>#define MAX 100int buf1[MAX];int buf2[MAX];pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;pthread_mutex_t mutex1=PTHREAD_MUTEX_INITIALIZER;pthread_cond_t cond=PTHREAD_COND_INITIALIZER;pthread_cond_t cond1=PTHREAD_COND_INITIALIZER;int size=0;int size1=0;void readData1(void){        pthread_mutex_lock(&mutex);        if(buf1[0]!=0)                pthread_cond_wait(&cond,&mutex);        else{                FILE *fp;                fp=fopen("1.dat","r");                while(!feof(fp)){                        fscanf(fp,"%d",&buf1[size]);                        ++size;                }        fclose(fp);        printf("Thread1 created!\n");        pthread_cond_signal(&cond);        }        pthread_mutex_unlock(&mutex);}void readData2(void){        pthread_mutex_lock(&mutex1);        if(buf2[0]!=0)                pthread_cond_wait(&cond1,&mutex1);        else{                FILE *fp;                fp=fopen("2.dat","r");                while(!feof(fp)){                        fscanf(fp,"%d",&buf2[size1]);                        ++size1;                }                fclose(fp);                printf("Thread2 created!\n");                pthread_cond_signal(&cond1);        }        pthread_mutex_unlock(&mutex1);}void process(void){        int i,j;         pthread_mutex_lock(&mutex);        pthread_mutex_lock(&mutex1);        if(buf1[0]==0){                pthread_cond_wait(&cond,&mutex);        }        if(buf2[0]==0){                pthread_cond_wait(&cond1,&mutex1);        }                for(i=0;i<size-1;i++)            for(j=0;j<size1-1;j++)              {                printf("Add:%d+%d=%d\n",buf1[i],buf2[j],buf1[i]+buf2[j]);                printf("Multiply:%d*%d=%d\n",buf1[i],buf2[j],buf1[i]*buf2[j]);              }        printf("Thread3 created!\n");        pthread_mutex_unlock(&mutex);        pthread_mutex_unlock(&mutex1);}int main(){        pthread_t t1,t2,t3;        pthread_create(&t1,NULL,(void*)readData1,NULL);        pthread_create(&t2,NULL,(void*)readData2,NULL);        pthread_create(&t3,NULL,(void*)process,NULL);        pthread_join(t1,NULL);        pthread_join(t2,NULL);        pthread_join(t3,NULL);        return 0;} 

热点排行