我在班级的示例中有这段代码,老师的指令说“使每个线程循环进行5次迭代”。我对如何使用此代码感到困惑:

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <semaphore.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <sys/utsname.h>

/* Symbolic Constants*/
#define NUM_THREADS 4
#define BUFFER_SIZE 10

/* Semaphore and Mutex lock */
sem_t cEmpty;
sem_t cFull;
pthread_mutex_t mutex;

/* Threads */
pthread_t tid; /* Thread ID */
pthread_attr_t attr; /* Thread attributes */

//prototypes
void *producer(void *param);
void *consumer(void *param);
int insert_item(int threadID);
int remove_item(int threadID);
void init();

/* Progress Counter and Thread IDs*/
int counter, pthreadID=0, cthreadID=0;

int main()
{
  /* Variables */
  int c1;

  /* Perform initialization */
  init();

  /* Create the producer threads */
  for(c1=0; c1<NUM_THREADS; c1++)
  {
    pthread_create(&tid, &attr, producer, NULL);
  }

  /* Create the consumer threads */
  for(c1=0; c1<NUM_THREADS; c1++)
  {
    pthread_create(&tid, &attr, consumer, NULL);
  }

  /* Ending it */
  sleep(2);

  printf("All threads are done.\n");

  /* Destroy the mutex and semaphors */
  pthread_mutex_destroy(&mutex);
  sem_destroy(&cEmpty);
  sem_destroy(&cFull);

  printf("Resources cleaned up.\n");

  exit(0);
}

void init()
{
  pthread_mutex_init(&mutex, NULL); /* Initialize mutex lock */
  pthread_attr_init(&attr); /* Initialize pthread attributes to default */
  sem_init(&cFull, 0, 0); /* Initialize full semaphore */
  sem_init(&cEmpty, 0, BUFFER_SIZE); /* Initialize empty semaphore */
  counter = 0; /* Initialize global counter */
}

void *producer(void *param)
{
  int x;
  for(x=0; x<5;)
  {
    sleep(1);

    sem_wait(&cEmpty); /* Lock empty semaphore if not zero */
    pthread_mutex_lock(&mutex);

    if(insert_item(pthreadID))
    {
      fprintf(stderr, "Producer error.");
    }
    else
    {
      pthreadID++;
      x++;
    }

    pthread_mutex_unlock(&mutex);
    sem_post(&cFull); /* Increment semaphore for # of full */
  }
return 0;
}

void *consumer(void *param)
{
  int y;
  for(y=0; y<5;)
  {
    sleep(1);

    sem_wait(&cFull); /* Lock empty semaphore if not zero */
    pthread_mutex_lock(&mutex);

    if(remove_item(cthreadID))
    {
      fprintf(stderr, "Consumer error.");
    }
    else
    {
      cthreadID++;
      y++;
    }
    pthread_mutex_unlock(&mutex);
    sem_post(&cEmpty); /* Increments semaphore for # of empty */
  }
return 0;
}

int insert_item(int threadID)
{
  if(counter < BUFFER_SIZE) /* Buffer has space */
  {
    counter++;
    printf("Producer %d inserted a cookie. Total:%d\n", threadID, counter);
    return 0;
  }
  else /* Buffer full */
  {
    return -1;
  }
}

int remove_item(int threadID)
{
  if(counter > 0) /* Buffer has something in it */
  {
    counter--;
    printf("Consumer %d removed a cookie. Total:%d\n", threadID, counter);
    return 0;
  }
  else /* Buffer empty */
  {
    return -1;
  }
}


有谁知道我在哪里添加for循环以“使每个线程循环进行5次迭代”?非常感谢您。

更新:我将while(1)更改为具有5次迭代的for循环,但是我仍然无法从insert_item和remove_item函数获取消息以打印5次,唯一的打印一次。有人知道我如何将其打印5次吗?

最佳答案

两个意思:

while(1) --> for (int i=0; i<5; i++)


但是我认为他想要这样:

void *producer(void *param)
{
  for(int i=0; i<5; ) // <----------------- for
  {
    // ...


    if(insert_item(pthreadID))
    {
      fprintf(stderr, "Producer error.");
    }
    else
    {
      i++; // <----------------------------- Increase i here
      // ...
    }

    // ...
  }
return 0;
}

void *consumer(void *param)
{
  for (int i=0; i<5; ) // <----------------- for
  {
    // ...

    if(remove_item(cthreadID))
    {
      fprintf(stderr, "Consumer error.");
    }
    else
    {
      i++; // <----------------------------- Increase i here
      // ...
    }

    // ...
  }
return 0;
}

10-07 15:20