无法获取互斥锁以防止线程访问函数

时间:2016-07-06 23:46:06

标签: c mutex semaphore consumer producer

为一个班级处理制作人和消费者问题,并且在进行最后修改时遇到麻烦。我遇到的问题是我认为我的互斥锁没有将我的线程锁定在函数之外。例如,如果我运行程序并传递参数2 4 4 7它将打印8 7 7秒然后2秒后将打印8 8 8秒然后8 9 9秒等等。我尝试过使用trylock并在信号量上移动,但无济于事。当导致没有任何线程被锁定时,是否存在我遗漏的东西?

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
typedef int buffer_item;
#define BUFFER_SIZE 5
#define TRUE 1


buffer_item START_NUMBER;
int counter;
int insert_item(buffer_item item);
int remove_item(buffer_item *item);  
buffer_item buffer[BUFFER_SIZE];
void* producer(void *ptr);
void* consumer(void *ptr);  
pthread_cond_t condc, condp;
pthread_mutex_t mutex;
int sleepTime, producerThreads, consumerThreads,a;
pthread_attr_t attr;
sem_t full, empty;

int insert_item(buffer_item item)
{
   if(counter < BUFFER_SIZE) {
      buffer[counter] = item;
      counter++;
      return 0;
   }
   else { 
      return -1;
   }
}
int remove_item(buffer_item *item)
{
   if(counter > 0) {
      *item = buffer[(counter-1)];
      counter--;
      return 0;
   }
   else { 
      return -1;
   }
}

void* producer(void *ptr) {
     buffer_item item;
     item = START_NUMBER;
   while(TRUE) {
      sleep(sleepTime);     
      sem_wait(&empty);
      pthread_mutex_lock(&mutex);    
      if(insert_item(item)) {
         fprintf(stderr, "error \n");
      }
      else {
         printf("producer%u produced %d\n", (unsigned int)pthread_self(),item);
         item++;
      }     
      pthread_mutex_unlock(&mutex);
      sem_post(&full);
   }
        }
void* consumer(void *ptr) {
   buffer_item item;
   while(TRUE) {
      sleep(sleepTime);
      sem_wait(&full);
      pthread_mutex_lock(&mutex);
      if(remove_item(&item)) {
         fprintf(stderr, "error \n");
      }
      else {
         printf("consumer%u consumed %d\n", (unsigned int)pthread_self(),item);
      }
      pthread_mutex_unlock(&mutex);
      sem_post(&empty);

   }
}
void initializeData() {
   pthread_mutex_init(&mutex, NULL);
   sem_init(&full, 0, 0);
    sem_init(&empty, 0, BUFFER_SIZE);
   pthread_attr_init(&attr);
   counter = 0;
}

int main(int argc, char **argv) {

    sleepTime = atoi(argv[1]);
    producerThreads = atoi(argv[2]);
    consumerThreads = atoi(argv[3]);
    START_NUMBER = atoi(argv[4]);
    initializeData();
    pthread_t pro, con;


    pthread_mutex_init(&mutex, NULL);
    pthread_cond_init(&condc, NULL); 
    pthread_cond_init(&condp, NULL); 

   for(a=0; a< consumerThreads;a++)
    pthread_create(&con, NULL, consumer, NULL);
    for(a=0;a<producerThreads;a++)
    pthread_create(&pro, NULL, producer, NULL);


    pthread_join(con, NULL);
    pthread_join(pro, NULL);


    pthread_mutex_destroy(&mutex); 
    pthread_cond_destroy(&condc); 
    pthread_cond_destroy(&condp); 
    sleep(sleepTime);

}

1 个答案:

答案 0 :(得分:1)

感谢Dmitri的帮助,在你告诉我关于和我的一个朋友的一些讨论后,我终于得到它开始输出正确的数字!

This is the output i have been looking for

foreach (List<double> row in array)
{
    foreach (double value in row)
    {
        value /= normalisationFactor;
    }
}