使用多线程进行错误参数解析-c

使用多线程进行错误参数解析-c,c,multithreading,pthreads,C,Multithreading,Pthreads,我正在从事一个项目,该项目主要是生产者-消费者,在循环缓冲区上具有pthread条件和互斥体。用户给出它想要的消费者和生产者线程的数量,随机数生成器种子,每个生产者产生的数字的数量以及循环缓冲区的大小。所有这些变量以及条件和互斥量都是全局变量 这是生产者线程代码 void * newProducer(void * t){ // Get the thread id int * a = (int *) t; int id = *a; printf("Producer

我正在从事一个项目,该项目主要是生产者-消费者,在循环缓冲区上具有pthread条件和互斥体。用户给出它想要的消费者生产者线程的数量,随机数生成器种子,每个生产者产生的数字的数量以及循环缓冲区的大小。所有这些变量以及条件和互斥量都是全局变量

这是生产者线程代码

void * newProducer(void * t){
    // Get the thread id
    int * a = (int *) t;
    int id = *a;
    printf("Producer thread %d started!\n", id);

    int i;
    //Get the seed
    //seed is global vairable(Random number generator seed
    unsigned int thread_seed = seed * id;
    //Allocate memory for the array to save the numbers
    int * numbers = (int *) malloc(sizeof(int) * n);

    //Open the files
    FILE * producer_file = fopen("prods_out.txt", "w");
    if(producer_file == NULL){
        printf("ERROR: fopen failed.\n");
        exit(1);
    }
    fprintf(producer_file, "-- PRODUCER DATA --\n");

    //Produce the numbers
    for( i = 0; i < n; i++){

        numbers[i] = rand_r(&thread_seed) % 100;

        //Lock mutex
        if(pthread_mutex_lock(&mutex) < 0){
            printf("ERROR: pthread_mutex_lock failed.\n");
            exit(1);
        }
        //printf("Producer thread <%d> locked mutex\n", *id);

        //Wait until the cb has space to store numbers
        while(cb->count == cb->capacity) {
            if(pthread_cond_wait(&cond, &mutex) < 0){
                printf("ERROR: pthread_cond_wait failed\n");
                exit(1);
            }
            printf("Producer <%d> waiting\n", id);
        }

        //Put the number in the cb
        cb_push_back(cb, &numbers[i]);
       // printf("Producer <%d>: %d\n", id, numbers[i]);
        fprintf(producer_file, "Producer <%d>: %d\n", id, numbers[i]);
        count++;
        //Count is a global variable to inform the consumers that there're 
        //no more numbers to be produced

        //Tell the consumer threads that you have put a number to the cb
        if(pthread_cond_broadcast(&cond) < 0){
            printf("ERROR: pthread_cond_broadcast failed.\n");
            exit(1);
        }

        //Unlock the mutex
        if(pthread_mutex_unlock(&mutex) < 0){
            printf("ERROR: pthread_mutex_unlock failed\n");
            exit(1);
        }
        //printf("Producer thread <%d> unlocked mutex\n", *id);
    }


    // Free array
    free(numbers);
    //Close File
    fprintf(producer_file, "-- END DATA --");
    if( fclose(producer_file) != 0){
        printf("ERROR: fclose failed.\n");
        exit(1);
    }

    //THREAD EXIT
    printf("PRODUCER %d EXITS\n", id);
    pthread_exit(t);
}
void*newProducer(void*t){
//获取线程id
int*a=(int*)t;
int id=*a;
printf(“生产者线程%d已启动!\n”,id);
int i;
//得到种子
//种子是全局可值的(随机数生成器种子
unsigned int thread_seed=seed*id;
//为阵列分配内存以保存数字
int*数字=(int*)malloc(sizeof(int)*n);
//打开文件
文件*producer\u FILE=fopen(“prods\u out.txt”、“w”);
if(producer_file==NULL){
printf(“错误:fopen失败。\n”);
出口(1);
}
fprintf(生产者文件,“--生产者数据--\n”);
//拿出数字
对于(i=0;i计数==cb->容量){
if(pthread_cond_wait(&cond,&mutex)<0){
printf(“错误:pthread_cond_wait失败\n”);
出口(1);
}
printf(“生产者等待”,id);
}
//把号码放在cb里
cb_推回(cb和编号[i]);
//printf(“生产者:%d\n”,id,编号[i]);
fprintf(生产者文件,“生产者:%d\n”,id,编号[i]);
计数++;
//Count是一个全局变量,用于通知消费者
//没有更多的数字要产生
//告诉消费者线程您已将一个数字添加到cb
if(pthread_cond_broadcast(&cond)<0){
printf(“错误:pthread_cond_广播失败。\n”);
出口(1);
}
//解锁互斥锁
if(pthread\u mutex\u unlock(&mutex)<0){
printf(“错误:pthread_mutex_unlock失败\n”);
出口(1);
}
//printf(“生产者线程解锁互斥\n”,*id);
}
//自由数组
免费(数字);
//关闭文件
fprintf(生产者_文件,“--结束数据-”);
如果(fclose(生产者文件)!=0){
printf(“错误:fclose失败。\n”);
出口(1);
}
//线程出口
printf(“生产者%d退出\n”,id);
pthread_exit(t);
}
这是消费者线索

void * newConsumer(void * t){
    // Get the thread id
    int * a = (int*) t;
    int id = *a;
    printf("Consumer thread %d started!\n", id);
    int  number;

    //Open the file
    FILE * consumer_file = fopen("cons_out.txt", "w");
    if(consumer_file == NULL){
        printf("ERROR: fopen failed.\n");
        exit(1);
    }
    fprintf(consumer_file, "-- CONSUMER DATA --\n");

    /*Consume integers until there are no integers left
    either to be produced or in the buffer waiting */
    while(count != p*n || cb->count != 0){
        //Lock the mutex
        //printf("Consumer <%d> has locked the mutex\n", *id);
        if (pthread_mutex_lock(&mutex) < 0){
            printf("ERROR: Could not lock mutex\n");
            exit(1);
        }

        //Wait until a producer puts an integer in the cb
        while(cb->count == 0 && end == 0){
            printf("Consumer <%d> waiting\n", id);
            if(pthread_cond_wait(&cond, &mutex) < 0){
                printf("ERROR: pthread_cond_wait failed.\n");
                exit(1);
            }
        }
        if (cb->count != 0){
            //Consume a number
            cb_pop_front(cb, &number);
           // printf("Consumer <%d>: %d\n", id, number);
            fprintf(consumer_file, "Consumer <%d>: %d\n", id, number);
        }
        //Tell the producers that you have popped a number from the cb
        if(pthread_cond_broadcast(&cond) < 0){
            printf("ERROR: pthread_cond_broadcast() failed.\n");
            exit(1);
        }
        //printf("Consumer <%d> Broadcast.\n", id);

        //Unlock the mutex
        //printf("Consumer <%d> has unlocked the mutex\n", *id);
        if(pthread_mutex_unlock(&mutex) < 0){
            printf("ERROR: pthread_mutex_unlock failed\n");
            exit(1);
        }
    }

    //Close the file stream
    fprintf(consumer_file, "-- END DATA --");
    if(fclose(consumer_file) != 0){
        printf("ERROR: fclose failed.\n");
        exit(1);
    }

    // THREAD EXIT
    printf("CONSUMER %d EXITS\n", id);
    pthread_exit(t);
}
void*newConsumer(void*t){
//获取线程id
int*a=(int*)t;
int id=*a;
printf(“消费者线程%d已启动!\n”,id);
整数;
//打开文件
FILE*consumer_FILE=fopen(“cons_out.txt”,“w”);
if(消费者_文件==NULL){
printf(“错误:fopen失败。\n”);
出口(1);
}
fprintf(消费者_文件,“--消费者数据--\n”);
/*使用整数直到没有整数为止
要生成或在缓冲区中等待*/
而(计数!=p*n | | cb->计数!=0){
//锁定互斥锁
//printf(“使用者已锁定互斥\n”,*id);
if(pthread\u mutex\u lock(&mutex)<0){
printf(“错误:无法锁定互斥\n”);
出口(1);
}
//等待生产者将整数放入cb
而(cb->count==0&&end==0){
printf(“消费者等待”,id);
if(pthread_cond_wait(&cond,&mutex)<0){
printf(“错误:pthread_cond_wait失败。\n”);
出口(1);
}
}
如果(cb->计数!=0){
//消耗一个数字
cb_pop_前端(cb和编号);
//printf(“消费者:%d\n”,id,编号);
fprintf(消费者文件,“消费者:%d\n”,id,编号);
}
//告诉制作人你已经从cb中弹出了一个号码
if(pthread_cond_broadcast(&cond)<0){
printf(“错误:pthread_cond_broadcast()失败。\n”);
出口(1);
}
//printf(“消费者广播。\n”,id);
//解锁互斥锁
//printf(“使用者已解锁互斥锁”;*id);
if(pthread\u mutex\u unlock(&mutex)<0){
printf(“错误:pthread_mutex_unlock失败\n”);
出口(1);
}
}
//关闭文件流
fprintf(消费者_文件,“--结束数据-”);
如果(fclose(消费者档案)!=0){
printf(“错误:fclose失败。\n”);
出口(1);
}
//线程出口
printf(“消费者%d退出\n”,id);
pthread_exit(t);
}
在我运行的主函数中

pthread_t * CONSUMERS[i] = (pthread_t*) malloc(sizeof(pthread_t) * i);
if (CONSUMERS == NULL){
    printf("ERROR: Malloc failed.\n");
    exit(1);
}


for (i = 0; i<c; i++){
    id[i] = i+1;
    pthread_create(&CONSUMERS[i], NULL, newConsumer, &id[i]);
}

pthread_t*使用者[i]=(pthread_t*)malloc(sizeof(pthread_t)*i);
if(使用者==NULL){
printf(“错误:Malloc失败。\n”);
出口(1);
}

for(i=0;i)类似于对齐问题。将pthread创建中的
&id[i]
更改为
(void*)&id[i]
,并查看是否有帮助。否,@alvits。假设
pthread\u create()的原型
在范围内,第四个参数从
int*
void*
的转换是隐式的和自动的。添加显式强制转换可能会强调这种转换的发生,但它根本不会改变程序的语义。@JohnBollinger-我完全同意假设pthread_create()的原型在范围内。因此没有人给出任何答案。我们只能从不完整的帖子中猜测并给出建议。我最好的猜测是有什么东西正在用不同的值覆盖
id
数组的元素。这种情况有很多种可能发生,特别是