C 多线程程序在完成前立即冻结

C 多线程程序在完成前立即冻结,c,multithreading,pthreads,mute,C,Multithreading,Pthreads,Mute,我编写了我的第一个多线程程序,并且在大多数情况下都能正常工作。共享缓冲区数组最初填充为-1,指示生产者它是空的,需要填充。然后,生产者用随机值1-10填充共享缓冲区,生产者轮流填充缓冲区。然后,生产者向消费者发出信号,表示它已经填充了缓冲区中的一个元素,并开始使用它。生产商需要填写120个元素,消费者应该填写每个条目。该程序在到达项目110之前运行良好。然后它冻结了,我不知道为什么。我该如何解决这个问题 下面是输出的一个片段 Item: 85, Consuming value 8, my th

我编写了我的第一个多线程程序,并且在大多数情况下都能正常工作。共享缓冲区数组最初填充为-1,指示生产者它是空的,需要填充。然后,生产者用随机值1-10填充共享缓冲区,生产者轮流填充缓冲区。然后,生产者向消费者发出信号,表示它已经填充了缓冲区中的一个元素,并开始使用它。生产商需要填写120个元素,消费者应该填写每个条目。该程序在到达项目110之前运行良好。然后它冻结了,我不知道为什么。我该如何解决这个问题

下面是输出的一个片段

 Item: 85, Consuming value 8, my thread id is: 1216
 Item: 86, Consuming value 7, my thread id is: 298320
Signal
Producer thread 231296 and value: 0
Producer thread 297552 and value: 2
Producer thread 298576 and value: 0
 Item: 87, Consuming value 9, my thread id is: 297808
Signal
Producer thread 960 and value: 3
Producer thread 298064 and value: 2
 Item: 88, Consuming value 3, my thread id is: 231744
 Item: 89, Consuming value 7, my thread id is: 298320
 Item: 90, Consuming value 3, my thread id is: 1216
 Item: 91, Consuming value 7, my thread id is: 298832
Signal
Producer thread 231296 and value: 3
Producer thread 297552 and value: 8
Producer thread 298576 and value: 6
 Item: 92, Consuming value 2, my thread id is: 297808
Signal
Producer thread 960 and value: 9
Producer thread 298064 and value: 7
 Item: 93, Consuming value 5, my thread id is: 298320
 Item: 94, Consuming value 2, my thread id is: 298832
 Item: 95, Consuming value 0, my thread id is: 1216
 Item: 96, Consuming value 2, my thread id is: 231744
这是我的密码

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <stdint.h>


#define THREADS 5
#define ELEMENTS 120


pthread_t tid_producer[THREADS], tid_consumer[THREADS];
int value = 0;
int saveValue = 0;
void *produce(void *arg);
void *consume(void *arg);
int producerCount =0;
int consumerCount = ELEMENTS;

struct {
  pthread_mutex_t mutex;
  int index;
  int value; 
  int MyShBuff[ELEMENTS];
} add = {PTHREAD_MUTEX_INITIALIZER, 0, 0}; 


  struct{
   pthread_mutex_t    mutex;
   pthread_cond_t     cond;
   int nready;
   int value;
   int empty;
   int counter;

   /* number ready for consumer */
} nready = {PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER,0, -2, ELEMENTS};

int main()
{

    int i, j, k;

    //Ready buffer for producers
    for (i =0; i < ELEMENTS; i++)
    {
        add.MyShBuff[i]=-1;
    }



    for(j = 0; j < THREADS; j++) {

   pthread_create(&tid_producer[j], NULL, &produce, NULL);
   pthread_create(&tid_consumer[j], NULL, &consume, NULL);
}



 /* wait for all producers and the consumer*/

    for(k = 0; k < THREADS; k++) {
        pthread_join(tid_producer[k], NULL);
        pthread_join(tid_consumer[k], NULL);    
    }

     /* Clean up and exit */

  pthread_mutex_destroy(&nready.mutex);
  pthread_mutex_destroy(&add.mutex);
   pthread_cond_destroy(&nready.cond);
   pthread_exit(NULL);

    exit(0);    
    return 0;
}

 void *produce(void *arg)
    { 
    int i = 0;


    for ( ; ; ) 
    {
        pthread_mutex_lock(&add.mutex);
        if(add.index  >= ELEMENTS)
        {

            pthread_mutex_unlock(&add.mutex);
            return NULL;
        }
        if(add.MyShBuff[add.index] == -1)
        {
         add.value = rand() % 10 + 0;   
         add.MyShBuff[add.index] = add.value;
         printf("Producer thread %d and value: %d\n" ,pthread_self(), add.MyShBuff[add.index]);
         add.index++;
        }
         pthread_mutex_unlock(&add.mutex);
         pthread_mutex_lock(&nready.mutex);

         if(nready.nready == 0)
         {
         pthread_cond_broadcast(&nready.cond);
         printf("Signal\n");
         }

        nready.nready++;
        pthread_mutex_unlock(&nready.mutex);

        }


    }


void *consume(void *arg)
{

pthread_mutex_lock(&nready.mutex);

  while(nready.empty != 0)
  {

    while (nready.nready == 0)
    {


        pthread_cond_wait(&nready.cond,&nready.mutex);


         pthread_mutex_lock(&add.mutex);
         printf(" Item: %d, Consuming value %d, my thread id is: %d\n", nready.counter, add.MyShBuff[nready.counter], pthread_self());
         add.MyShBuff[nready.counter] = -2;
         pthread_mutex_unlock(&add.mutex);

        nready.counter++;   
        nready.empty--;


    }

    nready.nready--;
   pthread_mutex_unlock(&nready.mutex);
  }


  return NULL;


}
#包括
#包括
#包括
#包括
#包括
#包括
#定义线程5
#定义元素120
pthread_t tid_生产者[线程]、tid_消费者[线程];
int值=0;
int saveValue=0;
作废*产生(作废*参数);
作废*消耗(作废*参数);
int producerCount=0;
int consumerCount=元素;
结构{
pthread_mutex_t mutex;
整数指数;
int值;
int MyShBuff[元素];
}add={PTHREAD\u MUTEX\u初始值设定项,0,0};
结构{
pthread_mutex_t mutex;
pthread_cond_t cond;
国际研究所;
int值;
int空;
整数计数器;
/*已为消费者准备好号码*/
}nready={PTHREAD_MUTEX_初始值设定项,PTHREAD_COND_初始值设定项,0,-2,ELEMENTS};
int main()
{
int i,j,k;
//为生产者准备的缓冲区
对于(i=0;i=元素)
{
pthread_mutex_unlock(&add.mutex);
返回NULL;
}
if(add.MyShBuff[add.index]=-1)
{
add.value=rand()%10+0;
add.MyShBuff[add.index]=add.value;
printf(“生产者线程%d和值:%d\n”,pthread_self(),add.MyShBuff[add.index]);
add.index++;
}
pthread_mutex_unlock(&add.mutex);
pthread_mutex_lock(&nready.mutex);
如果(nready.nready==0)
{
pthread_cond_广播(&nready.cond);
printf(“信号”);
}
nredy.nredy++;
pthread_mutex_unlock(&nready.mutex);
}
}
作废*消耗(作废*参数)
{
pthread_mutex_lock(&nready.mutex);
while(nready.empty!=0)
{
而(nready.nready==0)
{
pthread_cond_wait(&nredy.cond,&nredy.mutex);
pthread_mutex_lock(&add.mutex);
printf(“项目:%d,使用值%d,我的线程id是:%d\n”,nready.counter,add.MyShBuff[nready.counter],pthread_self());
add.MyShBuff[nready.counter]=-2;
pthread_mutex_unlock(&add.mutex);
nready.counter++;
nready.empty--;
}
nready.nready--;
pthread_mutex_unlock(&nready.mutex);
}
返回NULL;
}

正如注释中所指出的,在消费中使用互斥是错误的(想象一下,当条件错误时,线程将离开而不释放互斥) 此外,我建议限制包含相同信息(空,计数器)的变量的数量,这使得流很难跟随。 最后,pthread_wait_cond中还有一个bug: 请参阅或解释pthread_wait_cond用法的任何其他问题:它们强调谓词在返回时可能为true,这意味着您必须在开始任何要求谓词为true的计算之前再次测试它

我重写了一点你的代码让它工作;我希望我没有错过其他东西,但这可能会帮助你走得更远:

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <stdint.h>


#define THREADS 5
#define ELEMENTS 120


pthread_t tid_producer[THREADS], tid_consumer[THREADS];
int value = 0;
int saveValue = 0;
void *produce (void *arg);
void *consume (void *arg);
int producerCount = 0;
int consumerCount = ELEMENTS;

struct
{
    pthread_mutex_t mutex;
    int index;
    int value;
    int MyShBuff[ELEMENTS];
} add = { PTHREAD_MUTEX_INITIALIZER, 0, 0 };


struct
{
    pthread_mutex_t mutex;
    pthread_cond_t cond;
    int nready;
    int value;
    int empty;
    int counter;

    /* number ready for consumer */
} nready = { PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER, 0, -2, ELEMENTS, 0 };

int main(int argc, char **argv)
{
    //Ready buffer for producers
    for (int i = 0; i < ELEMENTS; i++) {
        add.MyShBuff[i] = -1;
    }


    for (int j = 0; j < THREADS; j++) {
        pthread_create (&tid_producer[j], NULL, &produce, NULL);
        pthread_create (&tid_consumer[j], NULL, &consume, NULL);
    }

    /* wait for all producers and the consumer */
    for (int k = 0; k < THREADS; k++) {
        pthread_join (tid_producer[k], NULL);
        pthread_join (tid_consumer[k], NULL);
    }

    /* Clean up and exit */
    pthread_mutex_destroy (&nready.mutex);
    pthread_mutex_destroy (&add.mutex);
    pthread_cond_destroy (&nready.cond);

    return 0;
}

void *produce(void *dummy)
{
    pthread_mutex_lock (&add.mutex);

    while (add.index < ELEMENTS) {
        if (add.MyShBuff[add.index] == -1)
        {
            add.value = rand () % 10 + 0;
            add.MyShBuff[add.index] = add.value;
            printf("Producer thread %ld and value: %d\n" ,pthread_self(), add.MyShBuff[add.index]);
            add.index++;
        }
        pthread_mutex_unlock (&add.mutex);

        pthread_mutex_lock (&nready.mutex);
        {
            if (nready.nready == 0)
            {
                pthread_cond_broadcast (&nready.cond);
                printf ("Signal\n");
            }
            nready.nready++;
        }
        pthread_mutex_unlock (&nready.mutex);

        pthread_mutex_lock (&add.mutex);
    }

    pthread_mutex_unlock (&add.mutex);

    return NULL;
}


void *consume(void *dummy)
{
    pthread_mutex_lock (&nready.mutex);

    while (nready.empty != 0)
    {
        // you also need to check it is not time to leave...
        while (nready.nready == 0 && nready.empty != 0) {
            pthread_cond_wait (&nready.cond, &nready.mutex);
        }

        if (nready.empty == 0) {
            break;
        }

        pthread_mutex_lock (&add.mutex);
        printf(" Item: %d, Consuming value %d, my thread id is: %ld\n", nready.counter, add.MyShBuff[nready.counter], pthread_self());
        add.MyShBuff[nready.counter] = -2;

        pthread_mutex_unlock (&add.mutex);

        nready.counter++;
        nready.empty--;

        nready.nready--;
    }

    pthread_mutex_unlock (&nready.mutex);

    return NULL;
}
#包括
#包括
#包括
#包括
#包括
#包括
#定义线程5
#定义元素120
pthread_t tid_生产者[线程]、tid_消费者[线程];
int值=0;
int saveValue=0;
作废*产生(作废*参数);
作废*消耗(作废*参数);
int producerCount=0;
int consumerCount=元素;
结构
{
pthread_mutex_t mutex;
整数指数;
int值;
int MyShBuff[元素];
}add={PTHREAD\u MUTEX\u初始值设定项,0,0};
结构
{
pthread_mutex_t mutex;
pthread_cond_t cond;
国际研究所;
int值;
int空;
整数计数器;
/*已为消费者准备好号码*/
}nready={PTHREAD_MUTEX_初始值设定项,PTHREAD_COND_初始值设定项,0,-2,元素,0};
int main(int argc,字符**argv)
{
//为生产者准备的缓冲区
for(int i=0;i