C线程互斥代码错误?

C线程互斥代码错误?,c,pthreads,mutex,C,Pthreads,Mutex,代码如下: .... 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){ fprin

代码如下:

....
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);
}
....
它应该是其他内容,而不是“(¬Empty,Null)”


请帮助。

当您已经有指针时,不应使用
&
。更改此项:

// 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
为此:

// 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
已经是一个指针,而make
&mut
则是试图获取指向
pthread\u mutex\t
notNull
的指针,该指针在两个位置写入
notFull
notNull
notEmpty
是条件变量,而不是互斥体,因此应进行初始化。 没有为
notNull
notEmpty
分配内存

最好将
队列
声明为:

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)

谢谢。问题i-iv怎么样?有什么想法吗?你应该在那里删除“&”。在所有地方,两个条件变量看起来都很难看,这通常意味着它不是一个理想的解决方案。您可以只使用一个条件变量“queueChanged”或其他内容,并在队列获得或丢失某个项目时标记它。
typedef struct {
  int buf[10];
  long head, tail;
  int full, empty;
  pthread_mutex_t mut;
  pthread_cond_t notFull;
  pthread_cond_t notEmpty;
} queue;