日期:2014-05-16  浏览次数:20634 次

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 100

int 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(&am