【问题标题】:Producers and Consumers with semaphores and threads使用信号量和线程的生产者和消费者
【发布时间】:2021-04-12 00:31:24
【问题描述】:

我目前遇到此代码的问题。代码中唯一出错的部分是“io_production_report(thread_args[0].Produced, pointer);”。

终端提到错误如下"

driver.cpp: In function ‘int main(int, char**)’:
driver.cpp:83:51: error: cannot convert ‘int (*)[2]’ to ‘int**’
   83 |     io_production_report(thread_args[0].Produced, pointer);
      |                                                   ^~~~~~~
      |                                                   |
      |                                                   int (*)[2]
In file included from driver.cpp:2:
io.h:49:48: note:   initializing argument 2 of ‘void io_production_report(int*, int**)’
   49 | void io_production_report(int produced[], int *consumed[]);
      |                                           ~~~~~^~~~~~~~~~
make: *** [Makefile:8: driver.o] Error 1

我在实现指针数组时遇到问题。 如果有人可以帮助我,那就太好了!! :

驱动程序.cpp

  // Print Production Report
    int pointer[2][2];
    pointer[0][0]=thread_args[0].Consumed[0]; //Ethel
    pointer[0][1]=thread_args[0].Consumed[1]; //Lucy
    pointer[1][0]=thread_args[1].Consumed[0];
    pointer[1][1]=thread_args[1].Consumed[1];

    io_production_report(thread_args[0].Produced, pointer);  //NEED HELP ON THIS LINE

驱动程序.h

struct args {
    sem_t *mutexOnBelt;
    sem_t *frogMax;
    sem_t *beltMax;
    sem_t *candiesOnBelt;
    sem_t *produceMax;
    sem_t *consumeMax;

    string *belt;
    int *head;
    int *tail;
    int wait_time;
    int produced;
    string *name;
    int Consumed[2];
    int onBelt[2];
    int Produced[2];
};

int option = 0;
int flagValues[4] = {0, 0, 0, 0};

io.c

/*
 * void io_production_report(int produced[], int *consumed[])
 * Show how many candies of each type produced.  Show how many candies consumed by
 * each consumer.
 * produced[] - count for each ProductType
 * *consumed[] - array of pointers to consumed arrays
 *    e.g. consumed[Lucy] points to an array that is indexed by product name
 */
void io_production_report(int produced[], int *consumed[]) {
  int p, c;  /* array indices */
  int total;

  printf("\nPRODUCTION REPORT\n----------------------------------------\n");

  /* show number produced for each producer / candy type */
  for (p=0; p < ProductTypeN; p++) {
    printf("%s producer generated %d candies\n",
       ProducerNames[p], produced[p]);
  }
  
  /* show number consumed by each consumer */
  for (c=0; c < ConsumerTypeN; c++) {
    printf("%s consumed ", ConsumerNames[c]);
    total = 0;
    for (p=0; p < ProductTypeN; p++) {
      if (p > 0)
    printf(" + ");
      total += consumed[c][p];
      printf("%d %s", consumed[c][p], ProducerAbbrevs[p]);
    }
    printf(" = %d total\n", total);
  }

  printf("Elapsed time %.3f s\n", elapsed_s());
}

【问题讨论】:

  • int array[N] 衰减到 int*int array[N][M] 衰减到 int (*)[M],而不是 int**。编译器向你展示了这个事实。
  • total += consumed[c][p]; 不会以您期望的方式工作。数组在内存中是平坦的。不知道第一个维度consumed[1] 不是您所期望的。
  • 我还是有点迷茫。你能告诉我怎么写吗?

标签: arrays c pointers consumer producer


【解决方案1】:

如 cmets 中所述,您需要创建指向数组第一个元素 id est 的指针:您将数组 [N][M] 指定为 int (*)[M],工作示例:

#include <stdio.h>

void io_production_report(int (*)[2]);

void io_production_report(int (*consumed)[2]) {
    for (int i = 0; i <4; i++)
        printf("%d\n", consumed[i/2][i%2]);
}

int main(void) {
    int pointer[2][2];
    pointer[0][0]=1; //Ethel
    pointer[0][1]=2; //Lucy
    pointer[1][0]=3;
    pointer[1][1]=4;

    io_production_report(pointer);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-13
    • 1970-01-01
    • 2013-11-19
    • 2017-03-18
    • 1970-01-01
    • 2018-09-22
    • 1970-01-01
    相关资源
    最近更新 更多