C 线程互斥代码错误?

发布于 2024-10-06 07:22:52 字数 1861 浏览 2 评论 0原文

这是代码:

....
typedef struct {
  int buf[10];
  long head, tail;
  int full, empty;
  pthread_mutex_t *mut;
  pthread_cond_t *notFull, *notEmpty;
} queue;

int main(){
  queue *que;
  pthread_t sup, cut;
  que = queueInit();
  if(que == NULL){
    fprintf(stderr, "Queue Init failed");
    exit(1);
  }
  pthread_create(&sup, NULL, insertQueue, (void*) que);
  pthread_create(&cut, NULL, insertQueue, (void*) que);
  pthread_join(sup,NULL);
  pthread_join(cut,NULL);
  queueDelete(que);
  return 0;
}

void *insertQueue(void *q)
{
  queue *que;
  int i;
  que = (queue *)q;
  for(i=0; i<20;i++){
    // Get mutex lock on the queue
    pthread_mutex_lock(&mut); // Question (i) I guess this line is wrong
    while(que>full){
      printf("Its full");
      // pthread wait condition for queue not full
      pthread_cond_wait(&notFull, &mut); // Question (ii)
    }
    queueAdd(que,i);
    // Unlock the queue
    pthread_mutex_unlock(&mut); // Question (iii)
    // Send signal saying there is data to be read
    pthread_cond_signal(&notEmpty); // Question (iv)
    usleeep(100000);)
    return(NULL);
  }
}

queue *queueInit(void){
  queue *q;
  q = (queue *)malloc(sizeof(queue));
  if(q==NULL) return (NULL);
  q->empty = 1;
  q->full = 0;
  q->head = 0;
  q->tail = 0;
  q->mut=(pthread_mutex_t *)malloc(sizeof(pthread_mutex_t));
  // Set default condition
  pthread_mutex_init(&mut,NULL); // Question v
  // Condition for not null
  pthread_mutex_init(&notNull,NULL); // Question vi
  // Condition for not empty
  pthread_mutex_init(&notEmpty,NULL); // Question vi
  return (q);
}
....

我已将我的问题放入代码中,即问题 i - vi

我的感觉告诉我,我的论点是错误的,例如问题 vi:

pthread_cond_init(notEmpty,NULL);

它应该是其他东西,而不是“(¬Empty,Null) ”。

请帮忙。

Here is the code:

....
typedef struct {
  int buf[10];
  long head, tail;
  int full, empty;
  pthread_mutex_t *mut;
  pthread_cond_t *notFull, *notEmpty;
} queue;

int main(){
  queue *que;
  pthread_t sup, cut;
  que = queueInit();
  if(que == NULL){
    fprintf(stderr, "Queue Init failed");
    exit(1);
  }
  pthread_create(&sup, NULL, insertQueue, (void*) que);
  pthread_create(&cut, NULL, insertQueue, (void*) que);
  pthread_join(sup,NULL);
  pthread_join(cut,NULL);
  queueDelete(que);
  return 0;
}

void *insertQueue(void *q)
{
  queue *que;
  int i;
  que = (queue *)q;
  for(i=0; i<20;i++){
    // Get mutex lock on the queue
    pthread_mutex_lock(&mut); // Question (i) I guess this line is wrong
    while(que>full){
      printf("Its full");
      // pthread wait condition for queue not full
      pthread_cond_wait(¬Full, &mut); // Question (ii)
    }
    queueAdd(que,i);
    // Unlock the queue
    pthread_mutex_unlock(&mut); // Question (iii)
    // Send signal saying there is data to be read
    pthread_cond_signal(¬Empty); // Question (iv)
    usleeep(100000);)
    return(NULL);
  }
}

queue *queueInit(void){
  queue *q;
  q = (queue *)malloc(sizeof(queue));
  if(q==NULL) return (NULL);
  q->empty = 1;
  q->full = 0;
  q->head = 0;
  q->tail = 0;
  q->mut=(pthread_mutex_t *)malloc(sizeof(pthread_mutex_t));
  // Set default condition
  pthread_mutex_init(&mut,NULL); // Question v
  // Condition for not null
  pthread_mutex_init(¬Null,NULL); // Question vi
  // Condition for not empty
  pthread_mutex_init(¬Empty,NULL); // Question vi
  return (q);
}
....

I have put my question in the code, i.e question i - vi

My feeling telling me, my arguments are wrong, for example the question vi:

pthread_cond_init(notEmpty,NULL);

it should be something else, not "(¬Empty,Null)".

Please help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绅士风度i 2024-10-13 07:22:53

notNull 在两个地方被写成 notFull
notNullnotEmpty 是条件变量而不是互斥体,应该这样初始化。
没有为 notNullnotEmpty 分配内存。

最好将 queue 声明为:

typedef struct {
  int buf[10];
  long head, tail;
  int full, empty;
  pthread_mutex_t mut;
  pthread_cond_t notFull;
  pthread_cond_t notEmpty;
} queue;

然后保留所有 & 字符。这意味着您可以通过一次调用来分配全部内存。

最后,我认为您的意思是 while(que->full) 而不是 while(que>full)

notNull is written as notFull in two places.
notNull and notEmpty are condition variables not mutexes and should be initialised as such.
No memory is allocated for notNull and notEmpty.

It may be better to declare queue as:

typedef struct {
  int buf[10];
  long head, tail;
  int full, empty;
  pthread_mutex_t mut;
  pthread_cond_t notFull;
  pthread_cond_t notEmpty;
} queue;

And then keep all the & characters. This means you can malloc the whole lot with a single call.

Lastly I think you mean while(que->full) not while(que>full).

他不在意 2024-10-13 07:22:53

当你已经有一个指针时,你不应该使用 & 。把这个:改成

// Set default condition
  pthread_mutex_init(&mut,NULL); // Question v
  // Condition for not null
  pthread_mutex_init(¬Null,NULL); // Question vi
  // Condition for not empty
  pthread_mutex_init(¬Empty,NULL); // Question vi

这个:

// Set default condition
  pthread_mutex_init(mut,NULL); // Question v
  // Condition for not null
  pthread_mutex_init(notNull,NULL); // Question vi
  // Condition for not empty
  pthread_mutex_init(notEmpty,NULL); // Question vi

注意&我已经收起来了。 mut 已经是一个指针,并且创建 &mut 是尝试获取指向 pthread_mutex_t 的指针

you should not use & when you already have a pointer. Change this:

// Set default condition
  pthread_mutex_init(&mut,NULL); // Question v
  // Condition for not null
  pthread_mutex_init(¬Null,NULL); // Question vi
  // Condition for not empty
  pthread_mutex_init(¬Empty,NULL); // Question vi

to this:

// Set default condition
  pthread_mutex_init(mut,NULL); // Question v
  // Condition for not null
  pthread_mutex_init(notNull,NULL); // Question vi
  // Condition for not empty
  pthread_mutex_init(notEmpty,NULL); // Question vi

Pay attention to & I have put away. mut is already a pointer, and making &mut is an attempt to get pointer to pointer to pthread_mutex_t

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文