C 线程互斥代码错误?
这是代码:
....
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 - 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
notNull
在两个地方被写成notFull
。notNull
和notEmpty
是条件变量而不是互斥体,应该这样初始化。没有为
notNull
和notEmpty
分配内存。最好将
queue
声明为:然后保留所有
&
字符。这意味着您可以通过一次调用来分配全部内存。最后,我认为您的意思是
while(que->full)
而不是while(que>full)
。notNull
is written asnotFull
in two places.notNull
andnotEmpty
are condition variables not mutexes and should be initialised as such.No memory is allocated for
notNull
andnotEmpty
.It may be better to declare
queue
as: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)
notwhile(que>full)
.当你已经有一个指针时,你不应该使用
&
。把这个:改成这个:
注意
&
我已经收起来了。mut
已经是一个指针,并且创建&mut
是尝试获取指向pthread_mutex_t
的指针you should not use
&
when you already have a pointer. Change this:to this:
Pay attention to
&
I have put away.mut
is already a pointer, and making&mut
is an attempt to get pointer to pointer topthread_mutex_t