我如何确保Producer和Consumer线程一个接一个地无限运行?

问题描述 投票:0回答:1

如何确保Producer和Consumer函数无限运行并且一个接一个地运行?例如:首先,我希望用户输入数组,然后使用者函数打印输入的数组,然后再次要求用户输入数组。

/*  Headers  */


pthread_mutex_t mutex;
pthread_cond_t cond,cond1;
void *producer(void *arg);
void *consumer(void *arg);
static int n;
int consumerFlag[100];

void *producer(void *arg) 
{

  pthread_mutex_lock(&mutex);        

  while(1)
    {
      printf("\n Enter no of terms");
      scanf("%d",&n);
      int i; 
      printf("\n Enter consumer flag array");
      for (i = 0; i < n; i++) 
        {
      scanf(" %d",&consumerFlag[i]);
      pthread_mutex_unlock(&mutex);           
        }
      pthread_cond_signal(&cond);    
      usleep(1000);
    }


}


void *consumer(void *arg) 
{
  int i;
  pthread_mutex_lock(&mutex);
  pthread_cond_wait(&cond, &mutex); 
  printf("\nConsumer Function"); 
  while(1)
    {

      printf("\nConsumer thread waiting"); 

      for (i = 0; i < n; i++) 
        {    
          printf("\nConsumerFlag %d = %d", i, consumerFlag[i]); 
          pthread_mutex_unlock(&mutex);         

        }          

    }

}

int main()
{
  int i=0;
  pthread_mutex_init(&mutex,0);
  pthread_cond_init(&cond,0);   
  pthread_t pThread, cThread;
  pthread_create(&pThread, 0, producer, 0);
  pthread_create(&cThread, 0, consumer,0);   
  pthread_join(pThread,NULL);
  pthread_join(cThread, NULL);
  pthread_mutex_destroy(&mutex);
  pthread_cond_destroy(&cond);
  return 0;
}

首先,我希望用户输入数组,然后使用者函数打印输入的数组,然后再次要求用户输入数组。

c linux pthreads
1个回答
0
投票

您的代码中几乎没有重大问题。

  1. 只有当线程之前已将其锁定时,线程才能解锁互斥锁。在您的代码中,互斥锁被锁定在循环函数之外并在循环内解锁。它会给出未定义的行为。
  2. 消费者函数在进入循环之前正在执行signal_wait()。我认为你已经假设生产者和消费者线程几乎同时启动,生产者在消费者锁定互斥锁之前不会发出信号。这是错的。您应该假设线程以随机顺序进行调度并处理所有可能的情况。
  3. 当消费者完全消费数据时,您不会通知生产者。从消费者到生产者,没有信号机制。
  4. 当生产者完全生成数据时,您正在通知消费者。但是消费者并没有在外面等待。这意味着,只有一旦它可以得到通知。
  5. 在生产者中,condition_signal是在解锁互斥锁之后,这是错误的。应该在锁定互斥锁的情况下调用信号。

因此在生产者中,您需要生成数据,向消费者发送信号并等待消费者确认数据被消耗。您需要另外设置一个标志,以考虑生产者线程在消费者线程之前获得锁定的情况。在这种情况下,当生产者发送信号时,消费者不会进入while循环。所以消费者无法捕捉到信号。

void *producer(void *arg) 
{     
    pthread_mutex_lock(&mutex);        

    while(1)
    {
        printf("\n Enter no of terms");
        scanf("%d",&n);
        int i; 
        printf("\n Enter consumer flag array");
        for (i = 0; i < n; i++) 
        {
            scanf(" %d",&consumerFlag[i]);
            //pthread_mutex_unlock(&mutex);  //Should not be unlocked in loop         
        }
        data_produced_flag = 1; //A flag to indicate data is ready. Should be initialized to 0 before thread creation. It is required if producer produces before consumer thread goes to pthread_cond_wait()
        pthread_cond_signal(&cond_producer); //Should send signal with mutex locked
        //usleep(1000); //Not required as it is going to wait for signal from consumer

        pthread_cond_wait(&cond_consumer, &mutex); //Wait for confirmation that data is consumed
    }
    pthread_mutex_unlock(&mutex); 
}

在使用者功能中,您应该检查数据是否已经生成。如果没有产生,等待来自生产者的信号。当数据可用时,消费并向生产者发出信号以获取新数据。

void *consumer(void *arg) 
{
    int i;
    pthread_mutex_lock(&mutex);
    //pthread_cond_wait(&cond, &mutex);  //Wait inside loop
    printf("\nConsumer Function"); 
    while(1)
    {
        if(0 == data_produced_flag) 
        {
            printf("\nConsumer thread waiting"); 
            pthread_cond_wait(&cond_producer, &mutex); //Wait for signal from producer
        }
        //else
        //{
            //Data is already produced. No need to wait.
        //}

        //Data is available for consumption
        for (i = 0; i < n; i++) 
        {    
            printf("\nConsumerFlag %d = %d", i, consumerFlag[i]); 
            //pthread_mutex_unlock(&mutex);    //We are in the process of consumption. Don't unlock mutex.
        }
        data_produced_flag = 0; //Reset the flag. Ready for new data.
        pthread_cond_signal(&cond_consumer); //Indicate that data is consumed
    }
    pthread_mutex_unlock(&mutex); 
}
© www.soinside.com 2019 - 2024. All rights reserved.