简体   繁体   English

检查 pthread_cond_t 的值

[英]Checking the value of pthread_cond_t

I am trying to check the value of a variable of type pthread_cond_t in a while loop condition.我正在尝试在 while 循环条件中检查 pthread_cond_t 类型的变量的值。

The variable is defined in a node struct with the following form:该变量在具有以下形式的节点结构中定义:

pthread_cond_t cv;

When I try to check the value of this using != or == NULL I get errors that this can't be done.当我尝试使用!=== NULL检查此值时,我收到无法完成的错误。 Here is what it looks like when I try to check it:这是我尝试检查时的样子:

while(!node->cv) {

where node is a pointer to a struct containing the cv.其中node是指向包含 cv 的结构的指针。

I get the error "wrong argument type to unary exclamation mark, since I guess it's not a boolean. is there a way to check if this condition variable has value?我收到错误“一元感叹号的参数类型错误,因为我猜它不是 boolean。有没有办法检查这个条件变量是否有价值?

Condition variables don't have values.条件变量没有值。 They're abstract objects you wait on and signal, and are abstractly associated with boolean-result expressions (predicates) involving other variables in your program that are protected by the associated mutex.它们是您等待和发出信号的抽象对象,并且抽象地与布尔结果表达式(谓词)相关联,这些表达式(谓词)涉及程序中受关联互斥锁保护的其他变量。 So instead of:所以而不是:

while (!node->cv)

you do:你做:

while (some_predicate(node))
    pthread_cond_wait(&node->cv, &node->mtx);

This has to be done with the mutex already locked.这必须在已锁定互斥体的情况下完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM